2

下面是我发送邮件的代码...

 public void SendBy(string to, string subject, string body)
{
    MailMessage nM = new System.Net.Mail.MailMessage();

    nM.To.Add("abc@compulynx.org");
    nM.Subject = subject;
    nM.Attachments.Add(new Attachment(oStream, Fname));
    nM.Body = body;
    System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
    System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

    SmtpClient client = new SmtpClient(settings.Smtp.Network.Host);
    client.Credentials = new NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

    client.EnableSsl = true;
    client.Send(nM);
}

这是我的网络配置代码...

<system.net>
     <mailSettings>
        <smtp deliveryMethod="Network" from="xyz@gmail.com" >
            <network host="smtp.gmail.com" 
                    defaultCredentials="false" userName="xyz@gmail.com" password="xyz"
                    port="587"/>
        </smtp>
    </mailSettings>
</system.net>

此代码工作正常,但我将如何更改发件人显示名称,通常我们这样使用

 mM.From = new MailAddress("xyz@gmail.com","xyz");

但就我而言,我没有在任何地方写发件人邮件地址,我只是从网络配置中获取,然后我将如何更改该名称..我的会话中有当前登录用户,我想将该名称显示为发件人。 ..

4

2 回答 2

6

should put it on smtp from tag

<smtp deliveryMethod="Network" from="XYZ&lt;xyz@gmail.com&gt;">
<network host="smtp.gmail.com" 
                defaultCredentials="false" userName="xyz@gmail.com" password="xyz"
                port="587"/>
于 2012-10-25T07:45:53.737 回答
0

你可以在 web.config 中这样设置它:

<network host="smtp.gmail.com" 
         defaultCredentials="false" 
         userName="&quot;xyz&quot;&lt;xyz@gmail.com&gt;"
         password="xyz" 
         port="587"/>

请参阅MailAddress 支持的格式

于 2012-08-27T08:22:10.213 回答