使用发件人发送电子邮件问题

时间:2014-10-17 07:56:41

标签: c# winforms email

我正在尝试将电子邮件发送到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");
        }

这是结果(作为图片),我将其上传到Dropbox,因为StackOverflow不允许我发布图片:

Link

谢谢

3 个答案:

答案 0 :(得分:0)

尝试做这样的事情并检查_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);

答案 1 :(得分:0)

建议您将FromTo传递给构造函数,如下所示:

var mail = new MailMessage("sender@hotmail.com", "recipient@hotmail.com");

ToFrom是只读的。

答案 2 :(得分:0)

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