i use the following code to send email :
public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
try
{
MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
var smtp = new SmtpClient
{
Host = "smtp.datagts.net",
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = new System.Net.NetworkCredential("MeEmail#...", "Password")
};
using (MailMessage message = new MailMessage(FromAddr, ToAddr)
{
Subject = Subject,
Body = Body,
IsBodyHtml = IsBodyHTML,
BodyEncoding = System.Text.Encoding.UTF8,
})
{
smtp.Send(message);
}
return true;
}
catch
{
return false;
}
}
It works on local and when i use my web site under my local IIS but when i upload it to my website it does not work and does not send email even any error occurs.
is there anybody out there to help me about this ?
UPDATE1 : i remove the try catch and catch an error with this message : Failure sending mail
UPDATE2 : I change my stmp server and use my Gmail account , look at this code :
public static bool SendEmail(string To, string ToName, string From, string FromName, string Subject, string Body, bool IsBodyHTML)
{
try
{
MailAddress FromAddr = new MailAddress(From, FromName, System.Text.Encoding.UTF8);
MailAddress ToAddr = new MailAddress(To, ToName, System.Text.Encoding.UTF8);
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("MeEmail#gmail.com", "Password")
};
using (MailMessage message = new MailMessage(FromAddr, ToAddr)
{
Subject = Subject,
Body = Body,
IsBodyHtml = IsBodyHTML,
BodyEncoding = System.Text.Encoding.UTF8,
})
{
smtp.Send(message);
}
return true;
}
catch
{
return false;
}
}
and now i get an error yet :(
I get the "MustIssueStartTlsFirst" error that mentioned in this link.
I am now trying to check #EdFS point and use port 25
UPDATE3: It is because i use the shared server , i just can change the port to 25 , and steel it does not work an give the same error, I am trying to get support from my server backup team
Assuming the SMTP server (smtp.datagts.net) is running fine, some items to check:
Your code seems to be using UseDefaultCredentials=true, but on the next line your are providing credentials
As mentioned in the comments check that Port 587 isn't blocked at your web host
If you are hosted on a shared server (not a dedicated machine), it's likely ASP.Net is set for medium trust. IF so, you cannot use any port for SMTP other than Port 25.
Update:
To try and get to the error. In your LOCAL (development) machine, add this to your web.config:
<system.web>
...
<securityPolicy>
<trustLevel name="Medium" />
</securityPolicy>
...
ASP.Net on your local machine runs in FULL TRUST. The above setting makes the current web site/application you are working on run in medium trust. You can remove/comment as necessary. The point of this exercise is to try and match what your web host settings are (it's obviously different if things work in your local machine then dies when published). It would be nice to just obtain the info from your web host..but until then....
Then try both Port 587 and 25.
It should fail on port 587 with a security exception (because of medium trust)
If your mail server only accepts SMTP connections on port 587, then of course, port 25 will not work either (but you should get a different error). The point being "...it still doesn't work..." in this case is that the SMTP server (smtp.datagts.net) only accepts connections on port 587
GMAIL is the same story. You cannot use Port 587 if your web host settings for ASP.Net is medium trust. I have been through this many times - it will "work" in my local machine, but as soon as I enable medium trust in my local development machine, it will fail.
You should ask your web host what their settings are for ASP.Net - if its some "custom" setting you can ask for a copy and use that in your dev box as well.
Related
I made a random Gmail account.I want that account to send to my personal Gmail account something, using C#. I found out about FluentEmail these days. This is the class:
public static class EmailSender
{
//the random account mail and password
private static string username = "blabla";
private static string password = "blabla";
static EmailSender()
{
NetworkCredential myCredentials = new NetworkCredential();
myCredentials.UserName = username;
myCredentials.Password = password;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = myCredentials,
Port = 465,
EnableSsl = true,
};
var sender = new SmtpSender(() => smtp);
Email.DefaultSender = sender;
}
public static async Task SendEmail(string body)
{
var email = await Email
.From(username)
.To("mymail")
.Subject("NEW BUG")
.Body(body)
.SendAsync();
if (email.Successful)
{
Acr.UserDialogs.UserDialogs.Instance.Alert("Your message was sent!", "Succesful", "Ok");
}
}
}
I don't know why but nothing happens when I click the send button.When I click it 2 times in a row the app crashes. I put a breakpoint at the start of the SendEmail function but I still don't know what's wrong. Maybe I set something wrong in the constructor?Thanks.
I'm not familiar with FluentEmail, but there are two points which are obviously problematic:
You check whether the mail has been sent successfully (if (email.Successful)). If it didn't, you just... do nothing. Instead, find out why the mail has not been sent and display that information instead. If your return object has a property Successful, I'm pretty sure it also has a property telling you what went wrong.
I did glimpse at the FluentEmail source code (based on your question), and it apparently uses .NET's built-in SmtpClient class. SmtpClient does not support SMTPS at port 465. The supported options are unauthenticated SMTP at port 25 or STARTTLS at port 587.
The goal is to send an email from a C# application. The code snippet below works for me, but when I run it at work (internet connection with a script configuration proxi.pac) it crashes.
Can someone help me?
string SendersAddress = "jd#gmail.com";
string ReceiversAddress = "dj#xxx.com";
const string SendersPassword = "xxxxxx";
const string subject = "Testing Gmail LOCAL";
const string body = "Hi This Is my Mail From Gmail";
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
//Timeout = 3000
};
MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
smtp.Send(message);
Console.WriteLine("Message Sent Successfully");
Console.ReadKey();
}
Try with port number 25.
Hope it works.
Try Port 465 Seems like port 587 is correct.
Note that some ISPs and proxy servers block SMTP data when it's not using port 25. This may be a deal-braker for you.
UPDATE
Seems like your proxy server is blocking your communication.
It is interesting to note that a simple Google search cannot give a help page from Google that has the expected SMTP settings, although there are pages for POP and IMAP settings.
Can we use gmail account to send email in asp.net website from *localhost * (local machine) ? I am trying but badly unsuccessful. It works fine on hosting but donot work on my machine.
I have windows server 2003 on my machine, I have added port 587 and 465 in firewall in exceptions. In my gmail account I also have enabled POP and IMAP. Some people suggest to use port 465 and others say port 587 should be used. I tried both and below was my result:
Using port 465 it take time and finally give message that the opration has timed out. falure
Using port 587 it dont take time, show message "failuer sending email" with an inner expection "No connection could be made because the target machine actively refused it 72.14.213.109:587"
Below is my code, please guide me where I am wrong or what I should do.
thanks
public static bool SendMail(string gMailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(gMailAccount);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Port = 587;
client.Send(msg);
return true;
}
catch (Exception e)
{
return false;
}
}
I use smtp.yandex.com. May be need change settings in your gmail account.
Try this firs: add:
client.DeliveryMethod = SmtpDeliveryMethod.Network;
If that does not help, your mailaddress might not be complete, use
var fromAddress = new MailAddress("from#gmail.com", "From Name");
and in the credentials use
Credentials = new NetworkCredential(fromAddress.Address, ....
Hope that helps
This SmtpClient has always worked for me:
new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("noreply#my_domain.com", "password")
};
And it matches yours.
Have you been able to contact smtp.gmail.com on port 587 through any other app? Telnet on that port maybe? Am thinking it's probably a network issue, although you did state that your firewall was wide open it's not the only hop between you and google.
I am writing a small utility to help process some MySQL tasks every night and have it email my personal email if it fails (this is a personal project, so no company smtp server or anything, emails going through public outlook accounts).
I tested about 5 times and each send was successful, but now any attempts to send email I get this exception:
Error sending test email: Transaction failed. The server response was: 5.2.0 STOREDRV.Submission.Exception:OutboundSpamException; Failed to process message due to a permanent exception with message WASCL UserAction verdict is not None. Actual verdict is Suspend, ShowTierUpgrade. OutboundSpamException: WASCL UserAction verdict is not None. Actual verdict is Suspend, ShowTierUpgrade.[Hostname=BY2PR0101MB1461.prod.exchangelabs.com]
A bit of an oops on my part - didn't think Outlook would consider it as spam on the 6th try - is there anything I can do in Outlook to correct this?
I am using a service account I created in outlook to send these emails to my personal inbox.
The actual code in question:
class JobMailer
{
private string email_to;
private string email_from;
private string password;
private string email_smtp;
private bool use_ssl;
private int port;
public void Send(string subject, string body)
{
MailMessage mail = new MailMessage(email_from, email_to);
using (SmtpClient client = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = use_ssl,
Host = email_smtp,
Timeout = 100000,
Port = port,
Credentials = new NetworkCredential(email_from, password)
})
{
mail.Subject = subject;
mail.Body = body;
client.Send(mail);
}
}
public JobMailer(string emailTo, string smtp, string emailFrom, string pw, int p, bool ssl)
{
email_to = emailTo;
email_from = emailFrom;
password = pw;
email_smtp = smtp;
port = p;
use_ssl = ssl;
}
}
I resolved this by verifying the account I was trying to use. Each time you encounter this error an email is sent to the account with instructions on what you need to do to resolve the error. Typically you will need to verify against a phone number.
Got this error trying to send lots of emails to myself at Outlook.com, using SMTP.
To fix it I simply added a 5 second delay between the sends, for example:
foreach(var mail in mailToSend)
{
await smtpClient.SendMailAsync(mail);
Console.WriteLine("Sent email: " + mail);
await Task.Delay(5000);
}
If you aren't doing this just as a test, then you can contact the Outlook.com team and ask them to whitelist your IP (make sure you have SPF, rDNS, etc. setup first).
I am using C# and .net for coding, to send bulk mails around 5000 mails at one shot using AWS(AMazon) SES(Simple Email Service) API, Everything is working fine if the number of mails sending are less than 500-600(approximately), but if it is more like 5000 it will send upto 500-600 and then it will stop sending emails. I have used datatable to store the mails list from database, assigned template body and subject, and then used for loop to send emails one by one. I need to know whether it is coding problem or for loop issue or some other thing ? Any suggestion is also helpful to me?
for (i = 0; i < dtable1.Rows.Count; i++)
{
Object a=dtable1.Rows[i]["student_emailid"];
String TO1=Convert.ToString(a);
TO1 = TO1.Trim();
if (TO1.Equals("")) {
continue;
}
const String FROM = "asit#amcsquare.com"; // Replace with your "From" address. This address must be verified.
String TO = TO1; // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
String SUBJECT = email_subject;
String BODY = templateBody;
// Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
const String SMTP_USERNAME = "XXXXXXXXXXXX"; // Replace with your SMTP username.
const String SMTP_PASSWORD = "XXXXXXXXXXXXXXXXXX"; // Replace with your SMTP password.
// Amazon SES SMTP host name. This example uses the us-west-2 region.
const String HOST = "email-smtp.us-east-1.amazonaws.com";
// Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
// STARTTLS to encrypt the connection.
const int PORT = 587;
// Create an SMTP client with the specified host name and port.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
//Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
//the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.Send(message1);
}
}
I don't know the exact reason why it is working now, but it's working. I changed the logic of the above code, it started working. Instead of fetching the SMTP connection each time, for sending each mail previously, this time I fetched the smtp connection only once and used it to send all the bulk mails at once and it started working.But the problem is the sending time, it is taking too much to send all the mails.Anyways I will find the solution for this also.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
client.EnableSsl = true;
for(i=0;i<mail.Count;i++)
{
String TO = mail[i];
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message1);
}
client.Dispose();
}
Label1.Text = mail.Count.ToString() + " mails sent !!";
}