System.Net.Mail SmtpClient c# - c#

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)

Related

Error on connect mail with mailkit?

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.

How to send an RFC822 file in c#

I'm trying to convert some Python code for use in a .Net website. The code retrieves a message stored in RFC822 format and sends the message to an SMTP server again using:
mail from: blah blah
rcpt to: blah blah
data
<send the text from the RFC822 file here>
.
So no parsing of the RFC822 file is required (fortunately!). In Python this is simply:
smtp = smtplib.SMTP(_SMTPSERVER)
smtp.sendmail(_FROMADDR, recipient, msg)
where the file has been read into the variable msg. Is there an easy way to do this in C#?
The built in C# SMTP objects don't offer a way to do this, or at least I haven't found a way. They seem to be based on the principle of building up a MailMessage by providing the addresses, subject and body separately. SmtpClient has a Send(string, string, string, string) method, but again this requires a separate subject and body so I guess it constructs the RFC822 formatted message for you.
If necessary I can write my own class to send the mail. It's such a simple requirement that it wouldn't take long. However if there is a way using the standard libraries they're probably less buggy than my code.
I would recommend using MimeKit to parse the message file and then use MailKit to send via SMTP. MailKit is based on MimeKit, so they work well together and MailKit's SmtpClient is superior to System.Net.Mail's implementation.
Parsing a message is as simple as this:
var message = MimeMessage.Load (fileName);
Sending the message is as simple as these few lines:
using (var client = new SmtpClient ()) {
client.Connect ("smtp.gmail.com", 465, true);
client.Authenticate ("username", "password");
client.Send (message);
client.Disconnect (true);
}
You're right, the inbuilt stuff doesn't offer a solution for this.
My advice would be to simply write a bit of code that uses TcpClient and StreamReader / StreamWriter to interact with the SMTP server. It shouldn't need more than 50 lines of code.
This is easy to do, you need only to install MailKit and MimeKit from nuget.
Pay attention because if you don't need authentication, setting "useSsl" to false is not enough, it doesn't work. You need to set MailKit.Security.SecureSocketOptions.None
var message = MimeMessage.Load("pathToEml");
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect("smtp.yourserver.yourdomain", 25, MailKit.Security.SecureSocketOptions.None); //set last param to true for authentication
//client.Authenticate("username", "password");
client.Send(message);
client.Disconnect(true);
}

ASP.NET 4.5 Setting SMTP Encoding in Config

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)
{
}

Sending email from c# code [duplicate]

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;

SmtpClient.SendAsync() does not work anymore

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.

Categories