在asp.net发送电子邮件发送密码而不是电子邮件

时间:2014-09-12 11:14:40

标签: c# asp.net

我正在尝试在用户注册成功后发送电子邮件。但它在FROM部分发送密码而不是发送电子邮件,如abc@gmail.com,它发送1234kjfh。我在这里错过了什么?

string name=txtfirstname.Text;
string fileName = Server.MapPath("~/App_Data/TextFile.txt");
string mailBody = File.ReadAllText(fileName);
mailBody = mailBody.Replace("##Name##", txtfirstname.Text);
mailBody = mailBody.Replace("##Email##", email);
mailBody = mailBody.Replace("##Phone##", txtphone.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Re: Activate your account for AIS FORUM";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("abc@gmail.com", "1234kjfh");
myMessage.To.Add(new MailAddress(txtemail.Text, email));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.EnableSsl = true;  
mySmtpClient.Send(myMessage);

Plz帮助!

2 个答案:

答案 0 :(得分:6)

constructor of MailAddress的第二个参数是displayName。您没有设置用于发送邮件的凭据,您正在设置显示名称。

要设置凭据,请使用SmtpClient.Credentials

SmtpClient client = new SmtpClient(server, port);
client.Credentials = new NetworkCredential("username", "password");

答案 1 :(得分:0)

你在这里遗漏了什么

new MailAddress(address,displayName)

所以,您已将显示名称添加为" 1234kjfh"

按以下步骤更新您的代码。

string name=txtfirstname.Text;
        string fileName = Server.MapPath("~/App_Data/TextFile.txt");
        string mailBody = File.ReadAllText(fileName);
        mailBody = mailBody.Replace("##Name##", txtfirstname.Text);
        mailBody = mailBody.Replace("##Email##", email);
        mailBody = mailBody.Replace("##Phone##", txtphone.Text);
        MailMessage myMessage = new MailMessage();
        myMessage.Subject = "Re: Activate your account for AIS FORUM";
        myMessage.Body = mailBody;
        myMessage.From = new MailAddress("abc@gmail.com", "abc@gmail.com");
        myMessage.To.Add(new MailAddress(txtemail.Text, email));
        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.EnableSsl = true;  
        mySmtpClient.Send(myMessage);

希望这有帮助。