2014-10-28 284 views
1

這是我的應用程序的總結: 我用c#做了這個應用程序,使用windows窗體,直到我添加了一個登錄表單(帶有2個用於電子郵件和密碼的文本框)我用任何電子郵件登錄...所以任何人都可以使用此應用程序發送電子郵件。 所以首先會顯示一個登錄表單,我需要輸入我的電子郵件和密碼,登錄按鈕被按下後,我的主窗體將顯示出來。用SMTP發送Gmail郵件

這是我的代碼:

代碼登錄形式:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using SendEmail.Classes; 
namespace SendEmail 
{ 
    public partial class Login : Form 
    { 
     public static string tb1; 
     public static string tb2; 

     public Login() 
     { 
      InitializeComponent(); 
      textBoxPassword.PasswordChar = '*'; 
     } 

     private void btnLogin_Click(object sender, EventArgs e) 
     { 
      Form1 form1 = new Form1(); 
      tb1 = textBoxEmail.Text; 
      tb2 = textBoxPassword.Text; 
      if (textBoxEmail != null && textBoxPassword != null) 
      { 
       //I use email and pass to take string values and put them into my main form, where I need to specify user's username and password 
       form1.email = tb1; 
       form1.pass = tb2; 
       form1.Show(); 
      } 
     } 
    } 
} 

代碼爲Form1(主窗體):

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Net.Mail; 
using System.Text.RegularExpressions; 
using SendEmail.Classes; 

namespace SendEmail 
{ 
    public partial class Form1 : DevExpress.XtraEditors.XtraForm 
    { 
     User user = new User(); 
     public string email; 
     public string pass; 
     public Form1() 
     { 
      InitializeComponent(); 
      DevExpress.Skins.SkinManager.EnableFormSkins(); 
     } 
     private void btnSend_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       //Error reports 
       #region Error Reports 
       Regex RX = new Regex("^[a-zA-Z0-9]{1,20}@[a-zA-Z0-9]{1,20}.[a-zA-Z]{2,3}$"); 
       if (!RX.IsMatch(textBoxTo.Text)) 
       { 
        errorProviderEmail.SetError(textBoxTo, "Email format is not correct"); 
        return; 
       } 
       if (textBoxSubject.Text == string.Empty) 
       { 
        errorProviderEmail.SetError(textBoxSubject, "Enter a subject"); 
        return; 
       } 
       #endregion 

       MailMessage mail = new MailMessage(); 
       SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587); 
       //System.Net.NetworkCredential auth = new System.Net.NetworkCredential(); 
       mail.From = new MailAddress(email); 
       mail.To.Add(textBoxTo.Text); 
       mail.Subject = textBoxSubject.Text; 
       mail.Body = richText.Text; 

       SmtpServer.Credentials = new System.Net.NetworkCredential(\*here I need the text from textBoxEmail (Login form) :*/ email, \*here I need the text from textBoxPassword (Login form) */ pass); 
       SmtpServer.EnableSsl = true; 

       #region Attachments 
       Attachment data = new Attachment(textBoxAttachment.Text); 
       Attachment data2 = new Attachment(textBoxAttachment2.Text); 
       Attachment data3 = new Attachment(textBoxAttachment3.Text); 
       mail.Attachments.Add(data); 
       mail.Attachments.Add(data2); 
       mail.Attachments.Add(data3); 
       #endregion 

       SmtpServer.Send(mail); 
       MessageBox.Show("mail Send"); 
       Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 

     private void textBoxTo_TextChanged(object sender, EventArgs e) 
     { 
      errorProviderEmail.Clear(); 
     } 

     private void textBoxSubject_TextChanged(object sender, EventArgs e) 
     { 
      errorProviderEmail.Clear(); 
     } 

     private void buttonUpload_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      if (dlg.ShowDialog() == DialogResult.OK) 
      { 
       textBoxAttachment.Text = dlg.FileName.ToString(); 
      } 
     } 

     private void buttonUpload2_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      if (dlg.ShowDialog() == DialogResult.OK) 
      { 
       textBoxAttachment2.Text = dlg.FileName.ToString(); 
      } 
     } 

     private void buttonAttachment3_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog dlg = new OpenFileDialog(); 
      if (dlg.ShowDialog() == DialogResult.OK) 
      { 
       textBoxAttachment3.Text = dlg.FileName.ToString(); 
      } 
     } 
    } 

的問題是,當我正在調試,它引發了一個例外:「參數'fileName'不能是一個空字符串」,所以我認爲我的電子郵件字符串沒有從textBoxEmail取得文本...

我希望我已經明確指出了我的問題:D如果我可以用我的程序插入圖片,它會更容易。

+2

使用您的調試器。哪一行是拋出這個錯誤?爲什麼有一個空字符串? 'fileName'聽起來不像是給我的電子郵件,你確定它不是把文件附加到電子郵件中的哪些行會拋出錯誤嗎?你打算每次都附上三個文件嗎?如果你的所有附件文本框中沒有東西,你會得到一個錯誤... – 2014-10-28 14:44:13

+0

這是我的錯誤:{「參數'fileName'不能爲空字符串。\ r \ n參數名稱:fileName」} – Valeriu92 2014-10-28 14:47:34

+0

顯然,一個或多個'textBoxAttachment.Text'變量是空的。你需要在創建'Attachment'對象之前檢查它。 – 2014-10-28 14:48:39

回答

0

我將假定其中一個Attachments fileName爲空字符串,並且因爲此異常被拋出。

 if(!string.IsNullOrEmpty(textBoxAttachment.Text.Trim())) 
     { 
      Attachment data = new Attachment(textBoxAttachment.Text.Trim()); 
      mail.Attachments.Add(data); 
     } 
     if(!string.IsNullOrEmpty(textBoxAttachment2.Text.Trim())) 
     { 
      Attachment data = new Attachment(textBoxAttachment2.Text.Trim()); 
      mail.Attachments.Add(data); 
     } 
     if(!string.IsNullOrEmpty(textBoxAttachment3.Text.Trim())) 
     { 
      Attachment data = new Attachment(textBoxAttachment3.Text.Trim()); 
      mail.Attachments.Add(data); 
     } 

這應該解決這個問題。

+0

Pffff ....這是多麼容易!非常感謝!!! – Valeriu92 2014-10-28 14:55:03

+0

@ user3183299歡迎您,如果您有所幫助,您可以接受答案!祝你的項目好運:) – mybirthname 2014-10-28 14:55:39