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

纵饮孤独 提交于 2019-12-02 08:35:02

问题


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;
    }
}

回答1:


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.




回答2:


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




回答3:


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");



来源:https://stackoverflow.com/questions/4243300/setting-a-different-from-address-for-mail-sent-via-gmail-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!