C# Can';不要通过Hotmail发送电子邮件,只能通过Gmail发送
我有个问题。我编写了一个程序,它可以让你从一个.txt列表向这个地址发送电子邮件。它有效(我在德国),但对其他人(在其他国家)无效。对他们来说,只有通过Gmail发送电子邮件才有效。有什么帮助吗 代码如下:C# Can';不要通过Hotmail发送电子邮件,只能通过Gmail发送,c#,email,smtp,hotmail,C#,Email,Smtp,Hotmail,我有个问题。我编写了一个程序,它可以让你从一个.txt列表向这个地址发送电子邮件。它有效(我在德国),但对其他人(在其他国家)无效。对他们来说,只有通过Gmail发送电子邮件才有效。有什么帮助吗 代码如下: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void cmdDistribute_Click(object sen
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdDistribute_Click(object sender, EventArgs e)
{
FileStream fs;
StreamReader sr;
if (Receiverlist.Text == "")
{
MessageBox.Show("Please type in the path of the list with the receiver! (.txt)", "ERROR : CANT FIND RECEIVERLIST!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
fs = new FileStream(Receiverlist.Text, FileMode.Open);
sr = new StreamReader(fs);
string zeile;
if (senderID.Text == "")
{
MessageBox.Show("Please type in your login information!", "ERROR : NO EMAIL ADDRESS!");
fs.Close();
return;
}
else if (SenderPassword.Text == "")
{
MessageBox.Show("Please type in your login information!", "ERROR : NO PASSWORD!");
fs.Close();
return;
}
MessageBox.Show("While sending the emails this programm won´t response till it has send \nall emails! This could take a while, so please be patient...", "ALERT", MessageBoxButtons.OK, MessageBoxIcon.Warning);
string mailProvider = "";
int port = 0;
try
{
if (ProviderGmail.Checked)
{
mailProvider = "smtp.gmail.com";
port = 587;
}
else if (providerHotmail.Checked)
{
mailProvider = "smtp.live.com";
port = 587;
}
}
catch (Exception ex)
{
MessageBox.Show("Can´t connect to the GMail / Hotmail server. \n Please contact TheFlash on skype!", "ERROR : SMTPSERVER", MessageBoxButtons.OK, MessageBoxIcon.Error);
fs.Close();
return;
}
if (EmailSubject.Text == "")
{
MessageBox.Show("Please type in the subject of the email!.", "ERROR : EMPTY SUBJECT", MessageBoxButtons.OK, MessageBoxIcon.Error);
fs.Close();
return;
}
int n = 1;
while (sr.Peek() != -1)
{
zeile = sr.ReadLine();
try
{
System.Net.Mail.MailAddress DistributorMail = new System.Net.Mail.MailAddress(senderID.Text);
System.Net.Mail.MailAddress Receiver = new System.Net.Mail.MailAddress(zeile);
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(DistributorMail, Receiver);
email.Subject = EmailSubject.Text;
email.Body = EmailBody.Text;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(mailProvider,port);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(senderID.Text, SenderPassword.Text);
if (checkBox1.Checked)
{
if (EmailAttachment.Text == "")
{
MessageBox.Show("Please type in the path of your attachment!.", "ERROR : EMPTY ATTACHMENT PATH", MessageBoxButtons.OK, MessageBoxIcon.Error);
fs.Close();
return;
}
else
{
MessageBox.Show("If you add an attachment to your mail, it will take longer to send it!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
email.Attachments.Add(new System.Net.Mail.Attachment(EmailAttachment.Text));
}
}
else
{
EmailAttachment.Enabled = false;
}
try
{
client.Send(email);
label9.Text = "Emails sent : " + n;
n = n + 1;
if (sr.Peek() == -1)
MessageBox.Show("Finished!", "TheFlash´s Email Distributor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (Exception Ex)
{
label9.Text = "Couldn´t send the emails!";
fs.Close();
return;
}
}
catch (Exception ex)
{
MessageBox.Show("This is not an valid email address.", "ERROR : WRONG EMAIL FORMAT", MessageBoxButtons.OK, MessageBoxIcon.Error);
fs.Close();
return;
}
}
fs.Close();
return;
}
private void cmdExit_Click(object sender, EventArgs e)
{
Close();
}
private void cmdAbout_Click(object sender, EventArgs e)
{
new AboutBox1().Show();
}
}
好的,出于这样或那样的原因,您的用户可能被锁定在他们的Hotmail帐户之外。因此,他们可以尝试通过正常的网页登录,看看是否要求他们解决一个难题来解锁它。有关详细信息,请参阅: 我不确定是否有可能对其进行编码。您可以在此处查看过去的问题: 另见:
您是使用TLS连接还是仅使用基本SSL连接?请阅读以了解我为什么编辑您的标题。@MikePrecup connect with basic SSL,它在代码人中…我去做了一些研究,您实际上使用的是TLS:,这很好,因为我相信hotmail需要它。有两种使用SSL的方法,您的代码只有一个EnableSsl标志,所以我问。不必像我没读问题一样对待我。@iAmFastAndYou在发送的try/catch中,您接受异常。在返回之前,您应该(以某种方式)记录消息和/或堆栈跟踪。希望这将提供更多关于问题所在的信息。谢谢,这可能就是问题所在。我有点困惑,因为他们都有问题:/我尝试了两天多没有睡觉来解决这个问题,哈哈哈,天哪,我太笨了^^再次感谢你,先生,祝你愉快!不客气。不要觉得自己很愚蠢:)最好不要接受这样的异常,因为在诊断/修复问题时,它们将是您的朋友。你今天也过得很好。当然不要觉得自己很愚蠢。我也有这个问题,这就是为什么我在回答的“也请看”部分写了这篇博文。很容易错过。