2014-10-17 136 views
0

我試圖將電子郵件發送到g-mail,它是成功的,但發件人與收件人相同。這是爲什麼?你們能幫我嗎?發送來自發件人的電子郵件問題

這裏是我使用的代碼:

public static void SendEmail(string _from, string _subject, string _body) 
     { 
      try 
      { 
       _message.From = new MailAddress(_from); 
       _message.To.Add(new MailAddress("[email protected]")); 
       _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("[email protected]", "Testing", "Testing"); 
     } 

這裏是結果(如圖像),我把它上傳到保管箱,因爲StackOverflow上不允許我張貼圖片:

Link

謝謝

+0

你確定'_from'變量與'_to'不同嗎? – 2014-10-17 08:06:02

+0

是的,因爲我在'contact ...'函數中將'_from'變量的值傳遞給'risna22sakura @ gmail.com',而'to'則在'SendEmail'函數中定義。 – 2014-10-17 08:09:48

+0

我的猜測是你有一個額外的smtp配置部分(app.config?),這些設置會覆蓋你在代碼中編寫的任何內容。你有配置文件中的某處smtp設置嗎? – DrCopyPaste 2014-10-17 08:14:48

回答

0
public static void SendEmail(string _fromEmail, string _fromEmailPassword, string _subject, string _body) 
     { 
      try 
      { 
       _message.From = new MailAddress(_fromEmail); 
       _message.To.Add(new MailAddress("[email protected]")); 
       _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

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); 
+0

還是不行的。 @LostCoder – 2014-10-17 08:36:07

+0

您確定要傳遞[email protected]的憑據嗎? – LostCoder 2014-10-17 08:46:50

0

建議你通過FromTo到構造函數,像這樣:

var mail = new MailMessage("[email protected]", "[email protected]");

ToFrom是隻讀的。

+0

Err,那麼'To'和'From'怎麼樣?一旦我刪除了這兩個並用上面的代碼替換它。出現錯誤信息並說'From必須指定' – 2014-10-17 08:35:50

+0

您可以用整個4行代替'_message'設置一個析構函數:var mail = new MailMessage(_from,「[email protected]」,_ subject ,_body)'如果還有其他的你會得到'從mus被指定'這對我來說會很奇怪 – szpic 2014-10-17 08:47:41

+0

這個'消息出現'在哪裏? – 2014-10-17 08:49:01