这是我的申请摘要: 我使用Windows窗体在c#中创建了这个应用程序,一切顺利,直到我添加了一个登录表单(带有2个电子邮件和密码的文本框),这允许我使用任何电子邮件登录...所以任何人都可以使用此应用程序发送电子邮件。 因此,首先会显示一个登录表单,我需要输入我的电子邮件和密码,按下“登录”按钮后,我的主表单将会显示。
这是我的代码:
登录表格代码:
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如果我可以使用我的程序插入图片会更容易。
答案 0 :(得分: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);
}
这应解决问题。