与发件人发送电子邮件问题
本文关键字:电子邮件 问题 | 更新日期: 2023-09-27 18:27:00
我正在尝试将电子邮件发送到g-mail,这是成功的,但是发件人与收件人相同。为什么会这样?你们能帮我吗?
这是我正在使用的代码:
public static void SendEmail(string _from, string _subject, string _body)
{
try
{
_message.From = new MailAddress(_from);
_message.To.Add(new MailAddress("fuhansxavega92@gmail.com"));
_message.Subject = _subject;
_message.Body = _body;
_smtp.Port = 587;
_smtp.Host = "smtp.gmail.com";
_smtp.EnableSsl = true;
_smtp.UseDefaultCredentials = false;
_smtp.Credentials = new NetworkCredential("username", "password");
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtp.Send(_message);
ShowMessageBox("Your message has been successfully sent", "Success", 2);
}
catch (Exception ex)
{
ShowMessageBox("Message : " + ex.Message + string.Empty, "Error", 1);
}
}
void contactToolStripMenuItem_Click(object sender, EventArgs e)
{
SystemManager.SendEmail("risna22sakura@gmail.com", "Testing", "Testing");
}
这是结果(作为图像(,我将其上传到保管箱,因为 StackOverflow 不允许我发布图像:
链接
谢谢
尝试做这样的事情,并从_from检查你的传递参数,
//To create MailMessage object
string strPassword = txtPassword.Text;
string strEmailAddress = txtFromEmailAddress.Text;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");//Set SMTP to gmail
mail.From = new MailAddress(strFromEmailAddress);//From side emailAdress
mail.To.Add(txtClientAddress.Text);//To side email Address
mail.Subject = strEmailTitle;//Subject to define
mail.Body = PreviewEmail();//Appending the body string to the Mail Body
mail.IsBodyHtml = true;
System.Net.Mail.Attachment attachment;//Set attachment object
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(strFromEmailAddress, strPassword);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
建议将From
和To
传递到构造函数中,如下所示:
var mail = new MailMessage("sender@hotmail.com", "recipient@hotmail.com");
To
和From
是只读的。
public static void SendEmail(string _fromEmail, string _fromEmailPassword, string _subject, string _body)
{
try
{
_message.From = new MailAddress(_fromEmail);
_message.To.Add(new MailAddress("fuhansxavega92@gmail.com"));
_message.Subject = _subject;
_message.Body = _body;
_smtp.Port = 587;
_smtp.Host = "smtp.gmail.com";
_smtp.EnableSsl = true;
_smtp.UseDefaultCredentials = false;
_smtp.Credentials = new NetworkCredential("_fromEmail", "_fromEmailPassword");
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtp.Send(_message);
ShowMessageBox("Your message has been successfully sent", "Success", 2);
}
catch (Exception ex)
{
ShowMessageBox("Message : " + ex.Message + string.Empty, "Error", 1);
}
我已经解决了这个问题,上面的代码就是它的解决方案。我的错误,从NetworkCredential
来看,应该是from the sender
,而不是to the recipient
password
。