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;
Related
I am working on an ASP .Net MVC website and I've to send email through Godaddy smtp, Previously my site was developed in classic ASP and it was hosted on godaddy's web hosting (then it was working fine) but now I am hosting this site on IIS,
I am using following code to send email but it is not working
MailMessage msg = new MailMessage();
msg.From = new MailAddress(model.From);
msg.To.Add(model.To);
msg.Body = model.Body;
msg.Subject = model.Subject;
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net",25);
smtp.Credentials = new NetworkCredentials("support#{myCompanyName}.com",{password});
smtp.EnableSsl = False;
smtp.Send(msg);
I have also used dedrelay.secureserver.net instead of relay-hosting.secureserver.net host (as mentioned at https://pk.godaddy.com/help/what-is-my-servers-email-relay-server-16601) but both are not working
GoDaddy does not allow relaying through their server unless you are on one of their hosting plans that includes SMTP.
You can set your credentials in webconfig like (for godaddy)
<system.net>
<mailSettings>
<smtp from="your email address">
<network host="relay-hosting.secureserver.net" port="25" />
</smtp>
</mailSettings>
</system.net>
and in c# you can use like
MailMessage message = new MailMessage();
message.From = new MailAddress("your email address");
message.To.Add(new MailAddress("your recipient"));
message.Subject = "your subject";
message.Body = "content of your email";
SmtpClient client = new SmtpClient();
client.Send(message);
It will work
Most SMTP servers are quite restrictive nowadays when it comes to outbound email. I recommend testing the parameters with an email client (or telnet, if you're into that kinda thing) before assuming that there is something wrong with the code. That might also give you an error message that helps debugging.
Some things that come to mind:
The server may check the FROM address against it's database, specifically the user account you use to authenticate. While you can put whatever you want in the header of the email, this field must be your the real address of the authenticated account and only that (no descriptive name).
The server may require the use of TLS encryption, regardless of the port.
Port 25 is quite common, but according to the official RFC mail submission should use port 587. Maybe try that.
It is possible that GoDaddy only allows connections from their own (hosting) servers to these SMTP relays.
Unless the connection fails completely (which would point to no. 4) the server should send some kind of error message at some point. As I wrote above, I would recommend testing/logging the communication, that should provide some insight.
Perhaps you should call up to your ISP, here in th Netherlands they mostly block port 25 because of malware and worms that used to send out email. It can be as simple as this. Have you tried telnetting from your local machine to the email server (telnet mailserver.io 25)? If this ends up in a time out you have your answer and the port is either filtered out at your ISP or from their end.
You can try this code
smtp.Host = "relay-hosting.secureserver.net"; smtp.EnableSsl = false; smtp.UseDefaultCredentials = false; smtp.Port = 25;
string From = "[MyGodaddyEMailAddress]"; //eg.info#mango.com
string FromPassword = "[MyGodaddyMailPassword]";
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(From);
msg.To.Add("[RecipientEmailAddress]");
msg.Subject = "[MailSubject]";
msg.Body = "[MailBody]";
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("mail.[domain].com", 587); //eg. mail.mango.com
smtp.Credentials = new System.Net.NetworkCredential(From, FromPassword);
smtp.EnableSsl = false;
// Sending the email
smtp.Send(msg);
// destroy the message after sent
msg.Dispose();
Console.WriteLine("Message Sent Successfully");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey();
I'm trying to send mail using SMTPClient in C#.
Everything was fine when I was sending mail using my Gmail account
using HOSTNAME: smtp.gmail.com
and PORT NUMBER: 587.
Here is the code which I used:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com",587)
//mail
mail.From = new MailAddress("someone#mydomain.com","myself");
mail.To.Add("itsme#gmail.com");
mail.Subject = "Test Mail C#";
mail.Body = "Hello";
mail.IsBodyHtml = true;
//smtpclient
SmtpServer.Port = 587;
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("someone#mydomain.com", "password");
Boolean MailSent=true;
try
{
SmtpServer.Send(mail);
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message);
MailSent = false;
}
finally
{
if (MailSent == true)
MessageBox.Show("mail sent");
else
MessageBox.Show("Failed to send mail");
}
Here are the details provided by the hosting service provider:
Secure SSL/TLS Settings (Recommended)
Username: someone#opastonline.com
Password: Use the email account’s password.
Incoming Server: mocha7004.mochahost.com
IMAP Port: 993
POP3 Port: 995
Outgoing Server: mocha7004.mochahost.com
SMTP Port: 465
Authentication is required for IMAP, POP3, and SMTP.
Non-SSL Settings
(This is NOT recommended.)
Username: someone#opastonline.com
Password: Use the email account’s password.
Incoming Server: mail.opastonline.com
IMAP Port: 143
POP3 Port: 110
Outgoing Server: mail.opastonline.com
SMTP Port: 25 --> When raised a ticket they also suggested me to use 2525 or 25
Authentication is required for IMAP, POP3, and SMTP.
But when I replace them using the details provided by my host provider, I am unable to push the mail.
It shows different error messages when I change port numbers (which I was given by the provider):
port 25: The remote certificate is invalid according to the validation procedure.
port 2525: Failure Sending Mail
port 465: Operation timed out
I cross checked every possible thing, but seems I am lost somewhere.
Go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps . So that you will be able to login from all apps.
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?
I am sending mail using this code
using System.Net.Mail;
using System.Net.Security;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc#gmail.com");
mail.To.Add("xyz#gmail.com");
mail.IsBodyHtml = true;
mail.Subject = "Email Sent";
mail.Body = "Mail Done";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "123456");
smtp.EnableSsl = true;
smtp.Send(mail);
Label1.Text = "Mail Sent";
Whem I am using abc#gmail.com(one email id) for sending email, mail will successfully send but when I am using pqr#gmail.com(another mail id) mail sending failed. On local server both "abc" & "pqr" working fine.
Please help me to sort out this problem.
Error Message
the smtp server requires a secure connection or the client is not authenticated the server response was 5.5.1 authentication requires
Update you code to the following:
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "123456");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
Try this line after the enableSSl=true code
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
Gmail has added a new security feature for not to allow sending email using the less secure apps. You have to change the settings for your gmail account to allow access by less secure apps using steps given by google at below link
https://support.google.com/a/answer/6260879?hl=en
Enable less secure apps to access accounts
1.Sign in to your Google Admin console.
Click Security > Basic settings.
Under Less secure apps, select Go to settings for less secure apps.
In the subwindow, select the Allow users to manage their access to less secure apps radio button.
Once you've set Allow users to manage their access to less secure apps to on, affected users within the selected group or Organizational Unit will be able to toggle access for less secure apps on or off themselves.
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.