I am new to azure. My asp .net MVC application hosted in azure. This application has email sending functionality. When the application is moved to azure, email functionality is not working. My error logs displays error as below:
System.Net.Mail.SmtpException: 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. a65sm9218660oih.6 - gsmtp
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
Code block used to send emails
SmtpClient client = new SmtpClient(GetStringValue(EMAIL_CLIENT), GetIntValue(EMAIL_PORT))
{
Credentials = new NetworkCredential(GetStringValue(EMAIL_USER_NAME), GetStringValue(EMAIL_PASSWORD)),
EnableSsl = sslOn
};
client.Host = SettingsManager.GetStringValue("EmailClient");//smtp.gmail.com
client.Port = SettingsManager.GetIntValue("EmailPort");//587
client.DeliveryMethod = SmtpDeliveryMethod.Network;
Can anyone please help me to resolve this problem. Thank you.
Even I faced the same issue with gmail. Gmail block your emails from azure, because your app is trying to log in from a location with different timezone or different from the one you used to create the account. Check your gmail inbox and it will have an email regarding blocked login attempt.
The solution is to either login to gmail from your azure server or
check the blocked email in your inbox and add that device to verfied
devices. ie select "I recognize this activity as mine" as mentioned in
this link.
This is fully working code from an app we had at one time hosted on Azure. The email sent fine through gmail
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Support#domain.com");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(email));
mail.Body = "Email body";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("gmail-email-address", "gmail-password");
smtp.EnableSsl = true;
smtp.Send(mail);
First check your code, there is issues on port, credentials, timeout, ...
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, "yourApplicationSpecificPassword"),
Timeout = 10000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
IsBodyHtml = true,
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Then in gmail setting, use 2 step verification -
https://myaccount.google.com/u/3/signinoptions/two-step-verification
and generate app passwd - https://myaccount.google.com/u/3/apppasswords
use generated passwd in code "yourApplicationSpecificPassword"
good luck!!
Related
I am beginner and don't have any idea to send emails using our own smtp server. I am trying to send Email using my SMTP server. and here is my code
public void SendEmail(string to, string subject, string body)
{
var fromaddr = FromAddress;
var password = Password;
MailMessage msg = new MailMessage
{
Subject = subject,
From = new MailAddress(fromaddr),
Body = body
};
msg.To.Add(new MailAddress(to));
SmtpClient smtp = new SmtpClient
{
Host = SmtpHost,
Port = 25,
UseDefaultCredentials = true,
EnableSsl = false
};
NetworkCredential nc = new NetworkCredential(fromaddr, password);
smtp.Credentials = nc;
smtp.Send(msg);
}
and in SmtpHost i store "localhost". and i have also installed smtp on my web server. and in iis if i choose "store in email directory". i got emails in given directory but if i choose deliver email to smtp server then i won't receive any email in my inbox.
on this option i am giving these values
smtp server : localhost
use localhost : checked
post : 25
authentication settings : not required
can you help me out?
Having researched the issue extensively especially applying recommendations from similar issues on stackoverflow, the below code still returns error ""System.Net.Mail.SmtpException: Syntax error, command unrecognized. The server response was: connection rejected at....""
try
{
var mail = new MailMessage();
mail.From = new MailAddress("demo77377#gmail.com");
mail.To.Add(new MailAddress("xxxxxxx#gmail.com"));
mail.Subject = "TEST";
mail.Body = "This is a test mail from C# program";
using (var smtp = new SmtpClient())
{
smtp.Credentials = new System.Net.NetworkCredential("demo77377#gmail.com", "AABBCCDDEE1!","gmail.com");
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Timeout = 10000;
//
smtp.Send(mail);
Console.WriteLine("Message sent successfully");
Console.ReadLine();
}
}
catch (Exception e)
I have done everything possible
On Client> I have alternated smtp properties (permutation) etc
On Server> I have made gmail account less secure, I have disabled captcha etc
I observed that similar issues on stackoverflow were mostly dated over 3years ago and thus, is it possible that gmail no longer supports this SMTP method via C#, likewise has it been deprecated in favor of gmail API
Also, please find provided in code, original password supplied for the gmail account, in order to confirm if this issue is general or isolated to this gmail account
Thanks
This code is verified to work fine:
var fromAddress = new MailAddress("YOUREMAIL#gmail.com", "YOUREMAIL#gmail.com");
var toAddress = new MailAddress("YOUREMAIL#gmail.com", "YOUREMAIL#gmail.com");
const string fromPassword = "YOURPASSWORD";
const string subject = "YOUREMAIL#gmail.com";
const string body = "YOUREMAIL#gmail.com";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
I'm not comfortable including your personal details even though you have so I have removed them. Obviously replace the email and password with the correct details.
Finally got it working after switching from my hotel's wireless network to office network...type of network connection has a role to play in sending email via SMTP in C#
I'm just trying to get my hmailserver to send mail from my C# program. The part that's killing me is the SSL part.
I originally got this error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.
So I added: smtp.EnableSsl = true; and now I get Server does not support secure connections.
Here is my code, this is driving me nuts. Do I have to create my own SSL or is there a way to disable SSL on hmailserver side?
MailMessage mail = new MailMessage("jlnt#ademo.net", "com", "NEW Item", emailBody);
SmtpClient smtp = new SmtpClient("1.1.1.250");
smtp.Port = 25;
NetworkCredential login = new NetworkCredential("ja#test.net", "dg");
smtp.Credentials = login;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
Ahh okay what you have to do is in HMailServer go to advanced- ip ranges. Create a new IP range for example if you 192.168.1.2, you have to make the range 192.168.1.1-192.168.1.3, then at bottom uncheck all the required smtp authentication boxes.
Annoying...
To enable secure connection to send email throught your email provider, you have to change the port number.
MailMessage mail = new MailMessage("jlnt#ademo.net", "com", "NEW Item", emailBody);
SmtpClient smtp = new SmtpClient("1.1.1.250");
//smtp.Port =25;
smtp.Port =587;
NetworkCredential login = new NetworkCredential("ja#test.net", "dg");
smtp.Credentials = login;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
i was having this issue, what i did was used localhost ip and EnableSsl to false
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "127.0.0.1";
smtpClient.Credentials = new NetworkCredential("test#123test.com", "pass123");
smtpClient.EnableSsl = false;
// then your other statements like: from, to, body, to send mail
this guide will help you setup custom NetworkCredentials in HMailServer as used above, hope helps someone.
I have stumbled on this question when trying to configure hMailServer to work to e-mail sending from C#. I have tried the following:
C# SmtpClient - does not work with implicit SSL - see this question and answers
AegisImplicitMail from here - could not make it work with UTF-8 strings (I have diacritics in my strings)
MailKit from here - very powerful and mature, no problems using it
I aimed for the following:
decent security
being able to send e-mails to mainstream e-mail providers (e.g. Google, Yahoo) and reach Inbox
being able to receive e-mails from mainstream e-mail providers
C# code
public void MailKitSend(string senderEmail, string senderName, string subject, string bodyText, string receivers, string receiversCc)
{
// no receivers, no e-mail is sent
if (string.IsNullOrEmpty(receivers))
return;
var msg = new MimeMessage();
msg.From.Add(new MailboxAddress(Encoding.UTF8, senderName, senderEmail));
msg.Subject = subject;
var bb = new BodyBuilder {HtmlBody = bodyText};
msg.Body = bb.ToMessageBody();
IList<string> receiversEmails = receivers.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string receiver in receiversEmails)
msg.To.Add(new MailboxAddress(Encoding.UTF8, "", receiver));
if (!string.IsNullOrEmpty(receiversCc))
{
IList<string> receiversEmailsCc = receiversCc.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string receiverCc in receiversEmailsCc)
msg.Cc.Add(new MailboxAddress(Encoding.UTF8, "", receiverCc));
}
try
{
var sc = new MailKit.Net.Smtp.SmtpClient();
if (!string.IsNullOrWhiteSpace(SmtpUser) && !string.IsNullOrWhiteSpace(SmtpPassword))
{
sc.Connect(SmtpServer, 465);
sc.Authenticate(SmtpUser, SmtpPassword);
}
sc.Send(msg);
sc.Disconnect(true);
}
catch (Exception exc)
{
string err = $"Error sending e-mail from {senderEmail} ({senderName}) to {receivers}: {exc}";
throw new ApplicationException(err);
}
}
hMailServer configuration
1) Opened ports - 25, 143, 465, 995 are opened to ensure that you can send and receive e-mail
2) TCP/IP ports configuration
SMTP / 0.0.0.0 / port 25 / no security (allow receiving start process)
SMTP / 0.0.0.0 / port 465 / SSL/TLS security (must define a SSL certificate)
POP3 / 0.0.0.0 / port 995 / SSL/TLS security (use the same SSL certificate)
3) pre C# testing
Run Diagnostics from hMailServer Administrator
Use an e-mail client that allows manual configuration of various settings such as ports for each protocol, security. I have used Thunderbird. Include sending of e-mails to external providers and receiving e-mails from them (I have tried with Gmail).
I made no changes in IP ranges and left the implicit ones (My computer and the Internet).
Although it's 7 years passed since the accepted answer was posted - I also upvoted it in the beginning - I want to emphasize that the suggested solution disables the whole authentication process which is unnecessary. The problem is the line with :
smtp.UseDefaultCredentials = false;
Just remove that line and it should work.
I post here the working solution for me (note that I'm not using SSL):
MailMessage mail = new MailMessage("a1#test.com", "foooo#gmail.com");
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("a1#test.com", "test");
client.Port = 25;
client.EnableSsl = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "...IPv4 Address from ipconfig...";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
I'm trying to send email with my website's address from a C# application.
This worked fine for several months until recently. (maybe my provider changes some things or someone else changed settings)
Here's the code:
private void sendEmail(Email invite) {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(smtpServerName);
mail.From = new MailAddress(emailUsername);
mail.To.Add(invite.RecipientEmail);
mail.Subject = invite.MessageSubject;
mail.Body = invite.MessageBody;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);
// SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
Here's the error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.
Looking at other questions I tried what they suggested, to make SmtpServer.EnableSsl = true. This didn't work at all. It gave the following:
System.Net.Mail.SmtpException: Server does not support secure connections.
I'm guessing I should disable SSL and have it the way it was before.
Any suggestions how to make email sending work again?
EDIT
I've tried without SmtpServer.UseDefaultCredentials = false;
I've tried with it set to true: SmtpServer.UseDefaultCredentials =true;
I've tried commenting that line along with the following //SmtpServer.Credentials = new System.Net.NetworkCredential(emailUsername, emailPassword);
That error message is typically caused by one of the following:
Incorrect connection settings, such as the wrong port specified for the secured or non-secured connection
Incorrect credentials. I would verify the username and password combination, to make sure the credentials are correct.
If you are sure that your Username and Password are correct, but you still get errors then it means that Gmail has blocked your application. Allow less secure apps in your Google Account settings
Visit this link to your Google Account settings for Less Secure App access.
Select your Google Account from which you are sending the mail.
Set Allow less secure apps to ON.
I think you have to set DeliveryMethod = SmtpDeliveryMethod.Network
this one is currently working in my PC, just i checked,working nice,try this
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
You need to create an app password so that your application can bypass 2FA:
Sign in with App Passwords
Google App Password
If you are deploying the application on a server, try logging in your google account in your server. Bizarre thing is, smtp email sending app works fine in my desktop. Not in the server. This might be affected by how google's security mechanism works.
I am trying to send mail from C# code using lotuslive smtp. But I have no success in sending the mail. everytime it says {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}.
My code is working fine for other email hosts like gmail and yahoo.
below is the code that I have used.
MailMessage message = new MailMessage();
message.From = new MailAddress("fromaddress");
message.To.Add(new MailAddress("toaddress"));
message.Subject = "Test";
message.Body = "test";
SmtpClient client = new SmtpClient("companyname-com-smtp.mail.lotuslive.com", 465);
client.UseDefaultCredentials = false;
NetworkCredential credential = new NetworkCredential("companycredentials", "password");
client.Credentials = credential;
client.EnableSsl = true;
try
{
client.Send(message);
}
catch(Exception ex)
{
}
Outgoing SSL SMTP Server: -smtp.mail.lotuslive.com (port: 465) Please
Note: Outgoing SMTP access for third party email clients is not
available for Trial accounts.
If it is trail account then may cause some problems.
MailClient = new SmtpClient();
MailClient.Host = "smtp.mail.lotuslive.com/your host address";
MailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailClient.Credentials = new System.Net.NetworkCredential(username, password);
MailClient.EnableSsl = true;
MailClient.Port = 465;
If you do not have demo account then Check this link - How to configure client in Outlook 2003.
Check these outlook configure settings match to your code settings.
If all this stuff is not the issue then it may be problem at your mail server. Check these links for information:
An existing connection was forcibly closed by the remote host in SMTP client
System.Net.Mail with SSL to authenticate against port 465