[英]Trying to send mail in C# but it doesn't seem to be working
因此,我想尝试使用Visual C#应用程序向自己发送一些邮件,但是它似乎只是在代码中运行(我知道这是因为我在代码末尾放置了一个消息框),并且什么也不发送。 出于明显原因,我确实更改了以下电子邮件信息。
这是我所拥有的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string Host = "smtp.live.com";
Int16 Port = 587;
bool SSL = true;
string Username = "myemail@hotmail.com";
string Password = "mypassword";
// Mail options
string To = "myemail@hotmail.com";
string From = "email@hotmail.com";
string Subject = "This is a test";
string Body = "It works!";
MailMessage mm = new MailMessage(From, To, Subject, Body);
SmtpClient sc = new SmtpClient(Host, Port);
NetworkCredential netCred = new NetworkCredential(Username, Password);
sc.EnableSsl = SSL;
sc.UseDefaultCredentials = false;
sc.Credentials = netCred;
MessageBox.Show("Test");
}
}
}
*请注意,我没有收到任何错误。
您实际上并没有发送邮件。
将此添加到您的代码中-您应该能够找出以下位置:
sc.Credentials = netCred;
try
{
sc.Send(message);
}
catch (Exception ex)
{
MessageBox(ex.ToString());
}
MessageBox.Show("Test");
添加这些名称空间...
using System.Net.Mail;
using System.Net;
using System.Configuration;
在要发送电子邮件的“代码隐藏”文件中添加以下代码行。
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("yourEmailId", "destinationEmailId");
mail.Subject = ""; //Enter the text for the subject of the mail in quotes.
mail.Body = ""; //Enter the text for the body of mail within the quotes.
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.live.com");
NetworkCredential cred = new NetworkCredential("yourEmailId", "yourPassword");
client.EnableSsl = true;
client.Credentials = cred;
try
{
client.Send(mail);
}
catch (Exception)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.