开发者

Setting a different "From" address for mail sent via Gmail using C#

开发者 https://www.devze.com 2023-01-26 20:30 出处:网络
I am using a simple mail sender class that uses the System.Net.Mail. I need to update my application so various users can send email via it (using the same smtp account) but the \"From\" address shoul

I am using a simple mail sender class that uses the System.Net.Mail. I need to update my application so various users can send email via it (using the same smtp account) but the "From" address should be of the user who is causing it to be sent. I tried setting the From property of MailMessage, and sending the from address into the constructor of MailMessage but nose of those worked. I am sure I am missing something simple or not understanding how the mail API works. Can anyone help?

Here my MailSender class that basically wraps the MailMessage, NetworkCredential and SmtpClient to provide one simple mail sending interface.

class MailSender
{
    private NetworkCredential credential;
    private String SenderAddress;
    private SmtpClient client;

    public MailSender(String ServerURL, String account, String password, String FromAddress = null, int port = -1, bool UseSSL = true)
    {
        if (port > 0)
        {
            client = new SmtpClient(ServerURL, port);
        }
        else
        {
            client = new SmtpClient(ServerURL);
        }
        credential = new NetworkCredential(account, password);
        client.UseDefaultCredentials = false;
        client.EnableSsl = UseSSL;
        client.Credentials = credential;

        if开发者_如何转开发 (FromAddress != null)
        {
            SenderAddress = FromAddress;
        }
        else
        {
            SenderAddress = account;
        }
    }

    public bool SendMessage(String to, String subject, String body)
    {
        try
        {
            MailMessage message = new MailMessage(SenderAddress, to, subject, body);
            message.From = new MailAddress(SenderAddress, "tester");

            message.IsBodyHtml = true;
            client.Send(message);
        }
        catch
        {
            return false;
        }
        return true;
    }
}


I just found out the answer by testing with another SMTP server. This is actually caused by GMail not allowing any other from address. This works fine with other SMTP servers.

Thanks to leppie, Mikael Svenson and smirkingman for their suggestions.


Gmail not allowing any other from address. It might help you, you can change the replay in this way:

mail.ReplyTo = new MailAddress ( "SomeOtherAddress@mycompany.com");


this thread is a bit old but I just found out that to be able to have a different address in the from field using Google servers to send emails from a script, you have to add that address to the Send As list in the Gmail account used to authenticate. So login to the Gmail account of the user you use to authenticate against Google servers, go to Config, Accounts and add the desired from addresses to the Send As list by following the 3 simple steps provided. That solved my problem Hope it helps someone Josu

0

精彩评论

暂无评论...
验证码 换一张
取 消