This question already has answers here:
Sending email through Gmail SMTP server with C#
(31 answers)
Closed 9 years ago.
I am trying to send an e-mail from my C# code.Here is an example
MailMessage message = new MailMessage();
message.To.Add("milos90zr#gmail.com");
message.Subject = "Registration";
message.From = new System.Net.Mail.MailAddress("milos90zr#hotmail.com");
message.Body = "OK";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Send(message);
But my code breaks and here is the error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. hn4sm3874638bkc.2 - gsmtp
You have to turn on encryption.
Put this line somewhere before smtp.Send()
smtpClient.EnableSsl = true;
Related
Does System.Net.Mail SmtpClient support smtp server with httpS or it work only with smtp servers with http?
I mean if i pass "https//:mysmtpserver" to constructor od SmtpClient instance - it would work?
SmtpClient works with SSL, yes. You need to provide it as a property like this:
var smtpClient = new SmtpClient("smtp.gmail.com")
{
Port = 587,
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true,
};
smtpClient.Send("email", "recipient", "subject", "body");
(taken from this article How to send emails from C#/.NET - The definitive tutorial)
my code is :
Pop3Client client = new Pop3Client();
client.Connect("pop.gmail.com", 995, true);
client.Authenticate("MyMailAccont#gmail.com", "Password");
....
error on Authenticate.eeror is:
Additional information: POP3 server did not respond with a +OK
response to the AUTH command.
my config is well.how to fix it?
gmail not work very well with pop3.
When receiving e-mails sent with System.Net.Mail.MailMessage some recipients seem to have encoding issues with the e-mail. For example charachter ä is displayed as ä. I have set encoding properties:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
...
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
What else can I do?
Update:
If I use the Body property the e-mail is displayed correctly, but when I use the AlternateViews property the e-mail is displayed incorrectly.
Complete code:
SmtpClient smtpClient = new SmtpClient("some.host.com");
MailMessage msg = new MailMessage();
msg.To.Add("someone#host.com");
msg.From = new MailAddress("name#host.com", "Name Name");
msg.Subject = "Test";
//Displays as ä
//msg.Body = "ä";
// Displays as ä
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("ä", new ContentType(MediaTypeNames.Text.Html));
msg.AlternateViews.Add(htmlView);
smtpClient.Send(msg);
When sending to Gmail the e-mail is displayed correctly, but when sending to an Exchange server the e-mail is displayed incorrectly. I have tested in .NET 3.5 and 4.5.
Try adding the content type to the Alternative View:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("ä", new ContentType(MediaTypeNames.Text.Html));
htmlView.ContentType.CharSet = Encoding.UTF8.WebName;
I don't think the BodyEncoding and SubjectEncoding affects reading of messages in any way - it applies to when you send messages and sets the text encoding for the messages and headers when messages are sent.
SmtpClient will read the content-type and charset encoding from messages sent and decode the content of the message and subject according to the encoding it finds in the headers.
If your message is not getting decoded, it seems that the messages are possibly not encoded correctly (or potentially double encoded UTF-8 encoded string getting encoded again by a message encoder), the request headers on the message don't properly match the actual encoding of the message or a charset format that isn't supported in .NET is used.
The only way to know though is to look at the actual raw request trace of a message that fails to see what's actually getting sent.
You might want to set up System.NET tracing for email messages (see http://codepaste.net/j52ktp) or else monitor the TCP/IP stream with something like Wireshark to see what's actually going over the wire and what the headers are instructing the client to do with the data.
FWIW, there's no reason if a message is properly UTF-8 encoded for SmtpClient to not read these messages correctly.
I am working on a website I didn't write so I don't know it inside out. The default setting for SMTP mail encoding is being set to ASCII (somewhere) but I want it to be UTF-8. It seems this should be in a config file but I can't find where it is specified. I'm aware that I can manually set it in the code but I want the change to be site wide. The emails are in html. Everywhere else on the site except for email seem to be correctly set to UTF-8.
Currently, my email headers have this setting for encoding:
Content-Type: text/html; charset=us-ascii
And it should be:
Content-Type: text/html; charset=UTF-8
or something to that effect.
Here is some sample code that is used. I don't think it is that informative. The email values are being produced in another class. This is the part that sends the email.
using System.Net.Mail;
....
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.From = new MailAddress(shopEmail, (Globals.Settings.Shop.MailName));
// This just pulls these values from another class
msg.Subject = editEmailTemplates.EmailSubject;
msg.Body = editEmailTemplates.EmailBody;
SmtpClient mailClient = new SmtpClient();
try
{
mailClient.Send(msg);
}
catch(SmtpException)
{
}
I have recently purchased a new computer, and now my e-mails never get sent, and there are NEVER any exceptions thrown or anything.
Can somebody please provide some samples that work using the SmtpClient class? Any help at all will be greatly appreciated.
Thank you
Updates
Ok - I have added credentials now. And can SUCCESSFULLY SEND e-mail synchronously. But I can still not send them asynchronously.
Old:
After trying to send e-mail synchronously, I receive the following exception:
Transaction failed. The server response was:
5.7.1 <myfriend#hotmails.com>: Relay access denied.
You can send mail through Async(). How means you should follow the below code,
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.SendAsync(mailMessage, mailMessage);
and, if you are using async, you need to also have the event handler like,
static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
//to be implemented
}
By using this, you can send Mail.
You could first try the synchronous Send method to verify that everything is setup correctly with the SMTP server and that you don't get any exceptions:
var client = new SmtpClient("smtp.somehost.com");
var message = new MailMessage();
message.From = new MailAddress("from#somehost.com");
message.To.Add(new MailAddress("to#somehost.com"));
message.Subject = "test";
message.Body = "test body";
client.Send(message);
Remark: In .NET 4 SmtpClient implements IDisposable so make sure you wrap it in a using statement.