[英]How C# SMTP works
I'm analyzing a C# project which have mailing functionality.我正在分析一个具有邮件功能的 C# 项目。 Due to some restrictions I cant run the project.
由于一些限制,我无法运行该项目。 I got few queries while analyzing the code
我在分析代码时遇到了几个问题
MailAddress toMailAddress = new MailAddress(strToMail);
MailAddress fromMailAddress = new fromMailAddress(strFromMail);
SmtpClient smtpClient = new SmtpClient(smtpServer);
String strBody = strMessage;
MailMessage msg = New MailMessage();
msg.From = fromMailAddress
msg.To.Add(toMailAddress);
msg.Subject = strSubject;
smtpClient.Send(msg);
Depending on how your SMTP
server is configured.取决于您的
SMTP
服务器的配置方式。 In the code you have posted, it seems to send email by the default port of SMTP
(maybe on 25
, 465
or 587
) without credentials.在您发布的代码中,似乎通过
SMTP
的默认端口(可能在25
、 465
或587
)发送 email 没有凭据。 It also could be configured at the config file (web.config, app.config).它也可以在配置文件(web.config、app.config)中进行配置。
No, your application runs under .net and outlook does not affect it.不,您的应用程序在 .net 下运行,并且 outlook 不会影响它。
It depends on the SMTP configuration, and maybe because u haven't configured, you're getting errors.这取决于 SMTP 配置,可能因为您尚未配置,您会遇到错误。 What was the exact error you were facing?
您面临的确切错误是什么? This was the snippet I used in my code for sending mail,the connection and credentials can be given by the following snippet, hope this helps!
这是我在代码中用于发送邮件的代码段,连接和凭据可以由以下代码段给出,希望对您有所帮助!
The outlook email shouldn't affect or cause any errors, according to my knowledge.据我所知,outlook email 不应该影响或导致任何错误。
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "server = YOURSERVERNAME; database = YOURDBNAME; User ID = sa; Password = YOURPASSWORD"; //Connection Details
//select fields to mail required details
SqlCommand sqlCommand = new SqlCommand("select Name,DOB,Email,Mob from Student", sqlConnection); //select query command
SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); //This is for creating a gmail client
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "Test SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.