I've seen a lot of examples of implicit SSL (for use with the deprecated System.Web.Mail), but I'm using System.Net.Mail and would like a code example for how to send SMTP emails via explicit SSL.
My current sendEmail code is below - note that if I set EnableSsl = true, server returns a 500 error and the email is never sent. I assume because this is implicit SSL, which System.Net.Mail does not support:
using System.Net.Mail;
...
public static void SendEmail(string emailbody)
{
MailMessage mailMessage = new MailMessage("myDomain#me.com", "yourDomain#me.com");
mailMessage.IsBodyHtml = true;
mailMessage.Body = emailbody;
mailMessage.Subject = "My email";
SmtpClient smtpClient = new SmtpClient("smtp.me.com", 25);
// if false, email sends. If true, email does not send, server returns 500 error
smtpClient.EnableSsl = false;
smtpClient.Send(mailMessage);
}
You can't.
https://support.microsoft.com/en-us/help/950260/you-cannot-use-system-net-mail-smtpclient-to-send-an-e-mail-message-wi
You can supposedly use cdo.sys, but it's really old and I don't believe it's supported anymore. I'm also not sure it's available or usable since it came with Windows 2000.
Your best bet would be to setup an actual mail server to handle talking to the outside world and talk to it with your unencrypted port 25 connection. If you have a small box to dedicate, Postfix is a free, very lightweight and very robust SMTP server that would work nicely.
Related
I've recently purchased the essentials email package from GoDaddy and I am trying to set up sending email from my website via SMTP. I have the following code set up.
var smtp = new SmtpClient
{
Host = "mycoolwebsite.com.mail.protection.outlook.com",
Port = 25,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("info#mycoolwebsite.com", "supersecurepassword")
};
using (var message = new MailMessage("info#mycoolwebsite.com", "myemail#test.com")
{
IsBodyHtml = true,
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
This gets to send and doesn't throw any exceptions, however the email is not sent. I am very confused at why this doesn't work.
Has anyone got any ideas?
This is going to require TLS which means using MailKit's SMTP. You can get it using the NuGet package manager in Visual Studio. Search for MailKit by Jeffrey Stedfast.
Documentation is here as well.
Once you have all the references in place, use the MailKit.Net.Smtp.SmtpClient class:
Set "smtp.office365.com" as your host
Use port 587.
You will need to add this line after creating your smtp instance because you have no OAuth token:
smtp.AuthenticationMechanisms.Remove("XOAUTH2");
That will do what you need.
Here's an example of what the whole thing should look like:
string FromPseudonym = "MySite Support";
string FromAddress = "admin#MySite.com";
var message = new MimeMessage();
message.From.Add(new MailboxAddress(FromPseudonym, FromAddress));
message.To.Add(new MailboxAddress("Recipient Pseudonym", "RecipientAddress#somewhere.com"));
message.Subject = "Testing Email";
var bodyBuilder = new BodyBuilder();
string MsgBody = "Message Body stuff goes here";
bodyBuilder.HtmlBody = MsgBody;
message.Body = bodyBuilder.ToMessageBody();
using (var client = new SmtpClient())
{
client.Connect("smtp.office365.com", 587);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(FromAddress, "Your super secret password goes here");
client.Send(message);
client.Disconnect(true);
}
You'll need the following namespaces to be included:
using MimeKit;
using MimeKit.Utils;
using MailKit.Net.Smtp;
I've been where you are before. If you are running the app from your dev environment, the emails will not be sent. GoDaddy SMTP is configured so that emails will only be sent when requested from within their environment.
If you push the code to their host and run it, it will work. The most painful thing about this is that everything appears to go smoothly, but the SMTP client just eats the request and leaves you wondering why no email is sent.
Not knowing anything about GoDaddy's API, the only thing I can suggest, is maybe verify that the port is correct. It might require transport layer security, in which case port 25 will be closed.
For example, I think the office365 SMTP server (smtp.office365.com) requires secure SMTP and uses port 587.
I want my application to send e-mail using 'SMTP over SSL' even if TLS is not supported by server.
So far I have tried
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("abc#xyz.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true; //true: sends using TLS, false: sends without security
SmtpServer.Send(mail);
MessageBox.Show("Mail sent");
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.ToString());
}
by setting the property called EnableSsl, I can send mail over the servers which support TLS but I am not able to send it through server which only supports SMTP over SSL.
How can I give support for this SMTP/SSL method?
According to the SMTPClient spec:
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx
The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.
You can try using System.Web.Mail.SmtpMail, which is deprecated, but which supports SSL:
https://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail(v=vs.110).aspx
TBH I think you should place a caveat on your service and state that only SMTP servers that use TLS are supported. But at the end of the day, that is up to you.
This link shows one more way that I can send email using SMTP over SSL with the help of Collaboration Data Objects component. This way also supports embedding images to email.
Please change your code..
SmtpServer.EnableSsl = false;
I'm working on an ASP.NET Web Forms app and I'm trying to programmatically send an email to myself. I'm using Gmail's SMTP client, and all is well except that when I send my message, I get this error:
"System.Net.Mail.SmtpException: The SMTP server requires a secure
connection or the client was not authenticated. The server response
was: 5.5.1 Authentication Required. Learn more at"
If I go into my gmail account settings and enable an option that allows me to allow access for "less secure apps", everything works fine. I'm wondering how I can send my email with having this option enabled.
protected void sendEmail(object sender, EventArgs e)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new System.Net.NetworkCredential("myusername#gmail.com", "mypassword"),
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true
};
MailAddress from = new MailAddress("myusername#gmail.com", "Torchedmuffinz");
MailAddress to = new MailAddress("myusername#gmail.com", "Torchedmuffinz");
MailMessage message = new MailMessage(from, to);
message.Subject = "test";
message.Body = "test";
Attachment attachFile = new Attachment(#"pathtofile");
message.Attachments.Add(attachFile);
try { client.Send(message); }
catch (Exception email_exception)
{
System.Diagnostics.Debug.WriteLine(email_exception);
}
}
Gmail port 587 does not support SSL.
I think the following code should work for you.
MailMessage msg = new MailMessage();
msg.From=new MailAddress("yourmail#gmail.com");
msg.To.Add("receiver#receiverdomain.com");
msg.Subject="Your Subject";
msg.Body="Message content is going to be here";
msg.IsBodyHtml=false; //if you are going to send an html content, you have to make this true
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port=587;
NetworkCredential credential=new NetworkCredential("yourmail#gmail.com","your gmail password");
client.UseDefaultCredentials=false;
client.Credentials=credential;
client.Send(msg);
There is a possibility to use Google SMTP servers without 'Allow less secure apps' option, but you cannot use your standard google username and password. See my instructions on other post:
Is there a way to use ASP.NET to send email through a Google Apps acccount without selecting the 'Allow less secure apps' option?
After I created a website on Arvixe, we'll call it
www.abcd.us
Accompanying the website, it also creates a email server called:
http://mail.abcd.us/
Because I need send email from this website, I created a email account named : wp#abcd.us
My question is : when I want to send an email from this website using c# code :
SmtpClient client = new SmtpClient("SERVER.arvixe.com", 465)
{
Credentials = new NetworkCredential("wp#abcd.us", "myAccountPassword"),
EnableSsl = true
};
MailAddress from = new MailAddress(#"wp#abcd.us", "wp");
MailAddress to = new MailAddress(#"ToAddress", "ToWho");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
// set subject and encoding
myMail.Subject = "111";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
myMail.Body = "Hi test ";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
myMail.IsBodyHtml = true;
client.Send(myMail);
When run at this statement:
client.Send(myMail);
The code always fail to send. I guess the smtp server may be wrong.
"SERVER.arvixe.com", 465
But I do not know what the correct server is. Where smtp server after I create a website on Arvixe?
Based on what I can see from Arvixe's Knowledge Base, there are two options. If you want to use SSL, you will need to use mail.webeasyserve.com port 465. If you don't want to use SSL, then use mail.abcd.com port 26.
However, this knowledge base article is two years old, so it is possible the information is out of date. Hopefully it will point you in the right direction, even if it is no longer correct.
The domain can also be used as "localhost" and another mail port 25 (non SSL). The mail server at Arvixe can also be accessed by mail.yourdomain.com .
Using the System.Net.Mail namespace the code used is as below.
MailMessage MyMailMessage = new MailMessage("example#gmail.com", "example#gmail.com",
"write your subject Here ", "Hi,This is the test message ");
MyMailMessage.IsBodyHtml = false;
NetworkCredential mailAuthentication = new NetworkCredential("example#gmail.com","xxxxxxxx");
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 465);
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
mailClient.Send(MyMailMessage);
Using the above code timeout exception happens if 465 port is used.
25 port works fine.
In the case of yahoo account both 465 and 25 gives failure sending mail.
Is there anyway 465 port can be supported for sending mails using gmail or yahoo account.
Refered the following link
http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx
Is states that Windows Mail uses System.Net.Mail to send messages - wont work with Implicit SSL.
Is there any solution to fix this problem.
Thanks in advance
This is not an answer to the problem but .NET built-in mail class doesn't support the needed implicit SSL method.You have to use third-party SMTP client components for this purpose which are capable of both explicit and implicit SSL.