2014-10-28 240 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