Send SMTP email using System.Net.Mail via Exchange Online (Office 365)

后端 未结 8 545
悲&欢浪女
悲&欢浪女 2020-11-29 23:34

We are testing the new Office 365 beta, and i have a mail account on the Exchange Online service. Now I\'m trying to connect a LOB application that can send smtp emails from

相关标签:
8条回答
  • 2020-11-30 00:29

    Quick answer: the FROM address must exactly match the account you are sending from, or you will get a error 5.7.1 Client does not have permissions to send as this sender.

    My guess is that prevents email spoofing with your Office 365 account, otherwise you might be able to send as sballmer@microsoft.com.

    Another thing to try is in the authentication, fill in the third field with the domain, like

    Dim smtpAuth = New System.Net.NetworkCredential(
        "TheDude", "hunter2password", "MicrosoftOffice365Domain.com")
    

    If that doesn't work, double check that you can log into the account at: https://portal.microsoftonline.com

    Yet another thing to note is your Antivirus solution may be blocking programmatic access to ports 25 and 587 as a anti-spamming solution. Norton and McAfee may silently block access to these ports. Only enabling Mail and Socket debugging will allow you to notice it (see below).

    One last thing to note, the Send method is Asynchronous. If you call

    Dispose
    immediately after you call send, your are more than likely closing your connection before the mail is sent. Have your smtpClient instance listen for the OnSendCompleted event, and call dispose from there. You must use SendAsync method instead, the Send method does not raise this event.


    Detailed Answer: With Visual Studio (VB.NET or C# doesn't matter), I made a simple form with a button that created the Mail Message, similar to that above. Then I added this to the application.exe.config (in the bin/debug directory of my project). This enables the Output tab to have detailed debug info.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.diagnostics>
            <sources>
                <source name="System.Net">
                    <listeners>
                        <add name="System.Net" />
                    </listeners>
                </source>
                <source name="System.Net.Sockets">
                    <listeners>
                        <add name="System.Net" />
                    </listeners>
                </source>
            </sources>
            <switches>
                <add name="System.Net" value="Verbose" />
                <add name="System.Net.Sockets" value="Verbose" />
            </switches>
            <sharedListeners>
                <add name="System.Net"
                  type="System.Diagnostics.TextWriterTraceListener"
                  initializeData="System.Net.log"
                />
            </sharedListeners>
            <trace autoflush="true" />
        </system.diagnostics>
    </configuration>
    
    0 讨论(0)
  • 2020-11-30 00:32

    Office 365 use two servers, smtp server and protect extended sever.

    First server is smtp.office365.com (property Host of smtp client) and second server is STARTTLS/smtp.office365.com (property TargetName of smtp client). Another thing is must put Usedefaultcredential =false before set networkcredentials.

        client.UseDefaultCredentials = False
        client.Credentials = New NetworkCredential("user@domain.com", "Password")
        client.Host = "smtp.office365.com"
        client.EnableSsl = true
        client.TargetName = "STARTTLS/smtp.office365.com"
        client.Port = 587
    
        client.Send(mail)
    
    0 讨论(0)
提交回复
热议问题