coggicc
coggicc

Reputation: 255

Send email using smtp and Outlook.Office365.com

I am getting hung up on sending emails using outlook.office365.com and ASP.NET.

I am passing in the email address from my contact form as the "FROM" email. If I change the "FROM" address from the contact form input to just "[email protected]" it works. Apparently there is a missing step if you are sending emails from a different domain that what the mail server is on.

Here is my error: "System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: 5.7.60 SMTP; Client does not have permissions to send as this sender". I looked this up and the results were pretty much telling me to set permissions in my Outlook client, which I am not using.

Please let me know where I'm going wrong. Thanks

I am using the following authentication to send my emails:

//msg.From = new MailAddress("[email protected]", "What's 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's 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("[email protected]", "mypassword"),
    EnableSsl = true
};

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

}

Upvotes: 3

Views: 12188

Answers (4)

ruchit
ruchit

Reputation: 230

In the below line

Credentials = new NetworkCredential("[email protected]", "mypassword"),

Remove "[email protected]" and use the actual From email address.

Upvotes: 1

Abdul Rauf
Abdul Rauf

Reputation: 972

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

Upvotes: 6

coggicc
coggicc

Reputation: 255

To solve my issue, I simply change my smtpClient code to the following:

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

We have IIS6 set up on the server the site is hosted with. We run the site through IIS7 but we run the mail server through IIS6. No usernames, no passwords. That's it.

Upvotes: -8

Martin Noreke
Martin Noreke

Reputation: 4136

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

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

Also, my host value for sending 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 that you have a valid email and password for the account you are sending from.

string sendFromEmail = "[email protected]";
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("[email protected]");

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

    client.Send(mail);
}

Hope this helps.

Upvotes: 6

Related Questions