简体   繁体   English

C# SMTP 的工作原理

[英]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);
  1. Here the from address is coming from database but no credentials stored in the DB for the corresponding email or passed through code either.这里的发件人地址来自数据库,但没有在数据库中存储相应 email 的凭据或通过代码传递。 Does that mean I can send email from any account without their credentials?这是否意味着我可以在没有凭据的情况下从任何帐户发送 email?
  2. Is outlook necessary to send mails?发送邮件需要 outlook 吗? If so, outlook is configured to different account then the from address in the code, will it throw any error?如果是这样,outlook 配置到不同的帐户然后代码中的发件人地址,它会抛出任何错误吗?
  1. 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的默认端口(可能在25465587 )发送 email 没有凭据。 It also could be configured at the config file (web.config, app.config).它也可以在配置文件(web.config、app.config)中进行配置。

  2. No, your application runs under .net and outlook does not affect it.不,您的应用程序在 .net 下运行,并且 outlook 不会影响它。

  1. 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!这是我在代码中用于发送邮件的代码段,连接和凭据可以由以下代码段给出,希望对您有所帮助!

  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM