设置不同"从"解决邮件使用C#通过Gmail发送 [英] Setting a different "From" address for mail sent via Gmail using C#

查看:134
本文介绍了设置不同"从"解决邮件使用C#通过Gmail发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是使用了 System.Net.Mail 一个简单的邮件发送者的类。我需要更新我的应用程序,使不同的用户可以通过发送电子邮件(使用相同的SMTP帐户),但在发件人地址应该是谁造成的呢要发送的用户。我尝试设置 MailMessage 属性,发送从地址到的构造函数MailMessage 但那些鼻工作。我相信,我失去了一些东西简单或不理解如何在邮件API的工作。任何人都可以帮忙吗?

下面,基本上围绕我的MailSender类 MailMessage 的NetworkCredential SmtpClient ,以提供一个简单的邮件发送界面。

 类MailSender
{
    私人的NetworkCredential的凭证;
    私人字符串SenderAddress;
    私人SmtpClient客户端;

    公共MailSender(字符串SERVERURL,字符串账号,密码字符串,字符串FromAddress =空,INT端口= -1,布尔UseSSL =真)
    {
        如果(端口大于0)
        {
            客户端=新SmtpClient(SERVERURL,口);
        }
        其他
        {
            客户端=新SmtpClient(SERVERURL);
        }
        凭据=新的N​​etworkCredential(账号,密码);
        client.UseDefaultCredentials = FALSE;
        client.EnableSsl = UseSSL;
        client.Credentials =凭证;

        如果(FromAddress!= NULL)
        {
            SenderAddress = FromAddress;
        }
        其他
        {
            SenderAddress =帐户;
        }
    }

    公共BOOL SendMessage函数(字符串,字符串主题,绳体)
    {
        尝试
        {
            MailMessage消息=新MailMessage(SenderAddress,收件人,主题,正文);
            message.From =新MailAddress(SenderAddress,测试);

            message.IsBodyHtml = TRUE;
            client.Send(消息);
        }
        抓住
        {
            返回false;
        }
        返回true;
    }
}
 

解决方案

我刚刚找到了答案通过与其他SMTP服务器测试。这实际上是通过GMail中不允许任何其它的地址引起的。这能与其他SMTP服务器。

由于leppie,史云逊的Mikael和smirkingman了他们的建议。

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.

这篇关于设置不同"从"解决邮件使用C#通过Gmail发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆