I'm unable to send any email to an external email address from my ASP.NET webpage.
Below is my code:
Dim oMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
Dim add As New MailAddress("emailaddy#external_address.com")
oMsg.From = New MailAddress("something#mydomain.com")
oMsg.To.Add(add)
oMsg.Subject = "Your Account Details & Information"
oMsg.Body = "my body text"
oMsg.IsBodyHtml = True
Dim smtp As New System.Net.Mail.SmtpClient("local_smtp_server", 25)
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
smtp.Credentials = New System.Net.NetworkCredential("auser#mydomain.com", "mypassword")
smtp.Send(oMsg)
is there anything I am missing? If I send to a local email it will work just fine. Is there something that I need to change on the MS Exchange Server?
Any code samples can be given in C# as well...
Related
I am trying to integrate SendGrid in ASP.NET MVC application using SmtpClient and MailMessage methods on Azure.
Code:
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Subject = "Subject";
message.To.Add("To#MyDomain.com");
message.Body = "Body";
message.From = new MailAddress("From#MyDomain.com", "From Name");
SmtpClient client = new SmtpClient("smtp.sendgrid.net");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("??", "SG.******");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 587; // I have tried with 25 and 2525
client.Timeout = 99999;
client.EnableSsl = false;
client.Send(message);
I end up with this issue both from C# console application and ASP.NET MVC application on Azure VM:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
As per the documentation avlb on SendGrid site: https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/recommended_smtp_settings.html
The UserName and Password has to : Use the string “apikey” for the SMTP username and use your API key for the password.
I have tried -
SendGrid UserName with which I login to their portal
The API Key from this page https://app.sendgrid.com/settings/api_keys
The password which starts with SG* which is as per their support team.
Any suggestions what am I missing or what which UserName/APIKey should I be using?
Tx!
The link says: Use the string “apikey” for the SMTP username and your API key for the password.
https://docs.sendgrid.com/for-developers/sending-email/v2-csharp-code-example#using-nets-built-in-smtp-library
The username was supposed to be "apikey" and not the actual "api key or api name"
client.Credentials = new NetworkCredential("apikey",
"<Your API Key which starts with SG.*>");
Adding "apikey" (facepalm) fixed my problem.
According to your description, I guess you may not add the VM outbound role for port 587.
I suggest you could follow below steps to add the role.
Find the network in azure vm portal.
Then find add outbound rule.
3.Add 587 port to outbound role.
Update:
I suggest you could try to use sendgrid SDK to do this.
I have create a test console application, it works well in my side.
Library: SendGrid
Codes as below:
var client = new SendGridClient("sendgridkey");
var from = new EmailAddress("send email address", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("To email address", "Example User");
var plainTextContent = $"Name : aaaa";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
client.SendEmailAsync(msg).Wait();
int i = 0;
i am trying to send email from my software using the smtp of yahoo but it shows the following error
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required".
I know about the less secure apps setting in google but i don't know about the settings in yahoo. the same code runs fine with the gmail account credentials. here is the code for reference.
string EmailFrom = "test#yahoo.com";
string EmailTo = "test#gmail.com";
string PassWord = "test123";
string EmailHost = "smtp.mail.yahoo.com";
string status = "";
string Body = "";
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(EmailFrom);
message.To.Add(new MailAddress(EmailTo));
message.Subject = "Auto Backup at test" ;
message.Body = "Backup has been taken at test on" + DateTime.Now;
Body = "Backup has been taken at test on" + DateTime.Now;
smtp.Port = 587;
smtp.Host = EmailHost;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(EmailFrom, PassWord);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//ServiceLogLibrary.WriteErrorlog("Step:5");
//Library.WriteErrorlog("Before sending mail");
smtp.Send(message);
Just providing some more information here as things have changed a little since the accepted answer was posted.
Go to the account security settings (https://login.yahoo.com/account/security)
Scroll to the bottom and search for the "Manage app passwords" heading as shown below and click it to add a new app.
Select an app type and generate the password. For API apps, use "Other App".
Use this password instead of your standard mailbox password when sending emails via your SmtpClient.
You need to go to
Go to your "Account security" settings.
Select Allow apps that use less secure sign in.
To deny or turn off app access, deselect the undesired app.
Source:Temporarily allow or deny access to apps using older security sign in
I have a program that sends eMail through the Exchange Webservice:
//EXCHANGE API
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
//BODY
string mBody = "<strong>TEXT</strong> Test";
//SETUP
ExchangeService service = new ExchangeService();
service.Url = new Uri("https://"+uriVar);
service.Credentials = new WebCredentials(userNameVar, passwordVar);
EmailMessage message = new EmailMessage(service);
message.Subject = mSubject;
message.Body = new MessageBody(BodyType.HTML,mBody);
message.ToRecipients.Add(recipient);
message.Save();
//Adding Image(s)
string fileFacebook = "facebook.png";
message.Attachments.AddFileAttachment("facebook.png", fileFacebook);
message.Attachments[0].IsInline = true;
message.Attachments[0].ContentId = "imgFacebook";
//Send
message.Send();
(the image is added in the original body, not in this example)
However, the eMail goes through as plain text, I cannot find out why. I am pretty sure it worked some time ago, is it possible that something has been changed on the Server? If so, what could it be?
I'm using EWS Managed API to sending email. Account "account#domain.com" have permissions "Send as" to use "sender#domain.com" mailbox to send messages (from Outlook, it's work fine).
But I try from code - it's not work, in mail i'm read in the field "From" "account#domain.com".
....
EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.From = txtFrom;
....
message.SendAndSaveCopy();
How to make sending mail on behalf of another user? :)
It's been a while since I fiddled with the same thing, and I concluded that it isn't possible, in spite of having "Send as" rights.
Impersonation is the only way to go with EWS, see MSDN:
ExchangeService service = new ExchangeService();
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("app#domain.com");
// impersonate user e.g. by specifying an SMTP address:
service.ImpersonatedUserId = new ImpersonatedUserId(
ConnectingIdType.SmtpAddress, "user#domain.com");
If impersonation isn't enabled, you'll have to supply the credentials of the user on behalf of whom you want to act. See this MSDN article.
ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("user", "password", "domain");
service.AutodiscoverUrl("user#domain.com");
Alternatively you can simply specify a reply-to address.
EmailMessage mail = new EmailMessage(service);
mail.ReplyTo.Add("user#email.com");
However, "Send as" rights do apply when sending mail using System.Net.Mail, which in many cases will do just fine when just sending e-mails. There are tons of examples illustrating how to do this.
// create new e-mail
MailMessage mail = new MailMessage();
mail.From = new MailAddress("user#domain.com");
mail.To.Add(new MailAdress("recipient#somewhere.com"));
message.Subject = "Subject of e-mail";
message.Body = "Content of e-mail";
// send through SMTP server as specified in the config file
SmtpClient client = new SmtpClient();
client.Send(mail);
i think you should use the Sender property so the your code should look like:
EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.Sender= txtFrom;
....
message.SendAndSaveCopy();
I have made an web application for sending e-mail. It works fine.
The problem is receiver end - Receiver shows NetworkCredential User Email as From Email.
And the email provided as From Email doesn't exist.
i want to show the suplied email not the networkcredential user email to the receiver.
sample code-
using System.Net.Mail;
MailMessage oMsg = new MailMessage();
oMsg.From = new MailAddress("sender#somewhere.com","Diplay Name");
oMsg.To.Add(new MailAddress("recipient#somewhere.com"));
oMsg.Subject = "Send Using Web Mail";
oMsg.Body ="Hi..";
System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient("host", port_no);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("user", "password");
s.EnableSsl = true;
s.UseDefaultCredentials = false;
s.Credentials = nc;
s.Send(oMsg);
The receiver gets from email is "user" but i want to show "sender#somewhere.com".
I think you need to update the Display name of the e-mail address that you send from.
Update oMsg.From = new MailAddress("sender#somewhere.com"); to be oMsg.From = new MailAddress("sender#somewhere.com","sender#somewhere.com");
MailAddress has an overload which allows you to pass is a display name for the given mail addres e.g. new MailAddress("sender#somewhere.com", "Display Name");
Some mail services (such as google), override the .FROM value, and will always use the ENVELOPE value, which is the NetworkCredential username.
I have a feeling that is what you are seeing.