Send email using smtp and Outlook.Office365.com

I am getting stuck sending emails using outlook.office365.com and ASP.NET.

I am passing the email address from my contact form as a "FROM" message. If I change the "FROM" address from entering the contact form to " no-reply@mydomain.com " it works. There seems to be a missing step if you are sending emails from a different domain that is running a mail server.

Here is my error: "System.Net.Mail.SmtpException: mailbox not available. Server response: 5.7.60 SMTP; client does not have permission to send as this sender." I looked at this and the results pretty much tell me that I am setting permissions in my Outlook client which I am not using.

Please let me know where I am going wrong. Thanks to

I am using the following authentication to send my emails:

//msg.From = new MailAddress("no-reply@plassonusa.com", "What Up");
//This is the From I want to use.  The 1st one is the only one that works
msg.From = new MailAddress(txtEmail.Text, "What Up");
msg.To.Add(emailTo);
msg.Subject = "my subject";

msg.IsBodyHtml = true;
msg.Body = "foo";

msg.Priority = MailPriority.High;

var smtpClient = new SmtpClient("outlook.office365.com", 587)
{
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,                              
    Credentials = new NetworkCredential("no-reply@domain.com", "mypassword"),
    EnableSsl = true
};

if (msg.To.Count > 0)
{
    try
    {
        smtpClient.Send(msg);
    }
    catch (Exception ex)
    {
        var error = ex.ToString();
    }

}

      

+3


source to share


4 answers


To solve my problem, I just change my smtpClient code to the following:

var smtpClient = new SmtpClient()
        {
            Host = "127.0.0.1"
        };

      



We have IIS6 installed on the server that hosts the site. We run the site over IIS7, but we run the mail server over IIS6. No usernames, no passwords. What is it.

-8


source


I fixed it using the same email id for "From" as the email id used as an SMTP user.



+6


source


One thing I see missing from your code that I have is the following for the SmtpClient configuration:

client.TargetName = "STARTTLS/smtp.office365.com";

      

Also, my send host value is "smtp.office365.com" instead of "outlook.office365.com".

EDIT:

I just built this test case from my working code. This will send an email assuming you have a valid email address and password for the account you are sending.

string sendFromEmail = "sendfrom@email.com";
string sendFromPassword = "TBD";

using (SmtpClient client = new SmtpClient("smtp.office365.com", 587))
{
    client.Credentials = new NetworkCredential(sendFromEmail, sendFromPassword);
    client.EnableSsl = true;
    client.TargetName = "STARTTLS/smtp.office365.com";

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(sendFromEmail);
    mail.To.Add("destination@email.com");

    mail.Subject = "Subject";
    mail.Body = "Test Email";
    mail.IsBodyHtml = false;

    client.Send(mail);
}

      

Hope it helps.

+4


source


In the next line

Credentials = new NetworkCredential("no-reply@domain.com", "mypassword"),

      

Delete " no-reply@domain.com " and use a valid email address.

0


source







All Articles