I have an Azure function in which I am trying to send an email:
EmailHelper.SendEmail(myEventHubMessage, notifyFrom, notifyTo, environment);
This is my SendEmail method:
MailMessage mail = new MailMessage(notifyFrom, notifyTo);
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "The actual value of host";
mail.Subject = $"[{environment}]: [{keyvaultEvent.Data.VaultName}] Key Vault Event";
mail.Body = $"An event of type {keyvaultEvent.EventType} in KeyVault {keyvaultEvent.Data.VaultName} occured at {keyvaultEvent.EventTime}";
client.Send(mail);
I tried running this locally and it is working fine. I get an email. After deploying the Azure function I get an error saying No such host is known
The values for environment, notifyfrom and notifyto are coming from app settings.
Any idea on why that is happening?
To send emails from Azure VMs or Apps, use an authenticated SMTP relay service, also known as a Smart Host, with TLS support.
SMTP relay services provide an intermediary SMTP server between the mail servers of the sender and recipient. The TCP connection is established via the secure ports 587 or 443.
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("user#recipient.com", "The Recipient"));
msg.From = new MailAddress("user#sender.com", "The Sender");
msg.Subject = "Test Email from Azure Web App using Office365";
msg.Body = "<p>Test emails on Azure from a Web App via Office365</p>";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("<Office365-username>", "<Office365-password>");#insert your credentials
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
For more details, you could refer to this article.
Related
I am using 'System.Net.Mail.SmtpClient' to send email notifications from my ASP.Net C# MVC web project using OFFICE365 EMAIL ID as Sender. It was working correctly a few months back but stop working when Microsoft Authenticator introduced for MS Apps. Please let me know in case you face this issue and better if you tell me it fixes.
SmtpClient client = new SmtpClient("smtp.office365.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("*****", "*****");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Timeout = 100000;
MailMessage msg = new MailMessage();
msg.To.Add("******");
msg.From = new MailAddress("*******");
msg.Subject = "Test";
msg.Body = "Testing";
client.Send(msg);
Exception => The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BM1PR0101CA0062.INDPRD01.PROD.OUTLOOK.COM]
I have written following code in codebehind aspx page to send email.I want to use google smtp server. But some how I am not receiving the mails
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Sender e-mail address.
MailAddress From = new MailAddress(txtFrom.Text);
// Recipient e-mail address.
MailAddress To = new MailAddress(txtTo.Text);
MailMessage Msg = new MailMessage(From,To);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = new System.Net.NetworkCredential
("*******#gmail.com", "**********");
client.EnableSsl = true;
client.Port = 465;
client.Send(Msg);
client.Dispose();
}
What wrong am I doing?Please help
Here is what you need to do with the SmtpClient object:
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("xxxx#gmail.com", "xxxx");
Gmail uses OAuth 2.0 you may well have to supply some sort of API key to access their mail functions.
Accessing mail using IMAP and sending mail using SMTP is often done using existing IMAP and SMTP libraries for convenience. As long as these libraries support the Simple Authentication and Security Layer (SASL), they should be compatible with the SASL XOAUTH2 mechanism supported by Gmail.
In addition to the SASL XOAUTH2 protocol documentation, you may also want to read Using OAuth 2.0 to Access Google APIs for further information on implementing an OAuth 2.0 client.
The Libraries and Samples page provides code samples in a variety of popular languages using the SASL XOAUTH2 mechanism with either IMAP or SMTP.
(source)
Here is a generic email program.
It works on Port=25.
Remember gmail is an IMAP server.
try
{
MailMessage msg = new MailMessage ();
MailAddress fromAdd = new MailAddress("fromemail#email.com");
msg.[To].Add("toemail#email.com");
msg.Subject = "Choose Session Members";
msg.From = fromAdd;
msg .IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg .BodyEncoding = Encoding.Default;
msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
msg.Body = msg.Body + "</table></center>";
SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("yourname#yourserver.com", "password");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
}
First of all, have you checked the webmail? In the sent folder? Many years ago I had the same problem, but I realized that my firewall was blocking me.
Other thing,
"SmtpClient class does not support Implicit SSL. It does support Explicit SSL, which requires an insecure connection to the SMTP server over port 25 in order to negotiate the transport level security (TLS)."
http://blog.ramsoftsolutions.com/2015/04/sending-mail-via-smtp-over-implicit-ssl.html
Source:
How can I send emails through SSL SMTP with the .NET Framework?
Regards
i config my outlook 2010 by thie article to send and receive email from yahoo.com it works good without any problem.
i develop a small application to send my emails by my application but it gave me errors:
"unable to read data from the transport connection:an exist connection was
forcibly closed by the remote host."
my codes:
try
{
SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com", 465);
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("myid", "mypass");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress("myid#yahoo.com", "blabla");
mailMessage.To.Add(new System.Net.Mail.MailAddress("xxx#live.com", "xxx#live.com"));
mailMessage.Subject = "test";
mailMessage.Body = "test";
mailMessage.IsBodyHtml = false;
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.Priority = MailPriority.High;
smtp.Send(mailMessage);
Console.WriteLine("hooooooooooraaaaaaaaaaaaaaa");
Console.ReadKey();
}
catch (Exception err)
{
Console.WriteLine(err.InnerException.Message);
Console.ReadKey();
return;
}
From MSDN
Some SMTP servers require that the client be authenticated before the
server sends e-mail on its behalf. Set this property to true when this
SmtpClient object should, if requested by the server, authenticate
using the default credentials of the currently logged on user. For
client applications, this is the desired behavior in most scenarios.
The UseDefaultCredentials = true sends to the SMTP server the credentials of the current logged in user (i.e. the Windows User) not the credentials you have defined.
Try with UseDefaultCredentials = false
What are needed to send an email from my asp.net site? this question asked because I have a site for sending email , it works correct in local machine , but when i uploaded it on a shared server it does not work and give me this error : Failure sending mail
now i have some question about this problem :
1 - Is it SSL always required to send an email by SMTP?
2 - what about the first question if i use my gmail account?
3 - is there some configuration on my host PleskPanel to send email ?
4 - after contact with my host support team they said me that my code has problem , but i know my code is correct , here is my code
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
client.Host = "smtp.datagts.net";
client.Port = 587; // setup Smtp authentication
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("info#datagts.net","*****");
client.UseDefaultCredentials = false;
client.Credentials = credentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress("info#datagts.net");
msg.To.Add(new MailAddress("foroughi.ali#gmail.com"));
msg.Subject = "Test";
msg.IsBodyHtml = false;
msg.Body = "Body is here";
client.Send(msg);
Note : for more info about my problem i asked another question about this issue that i have not get any correct answer
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