Jay
Jay

Reputation: 41

How C# SMTP works

I'm analyzing a C# project which have mailing functionality. 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. Does that mean I can send email from any account without their credentials?
  2. Is outlook necessary to send mails? If so, outlook is configured to different account then the from address in the code, will it throw any error?

Upvotes: 0

Views: 349

Answers (2)

Felipe Oriani
Felipe Oriani

Reputation: 38638

  1. Depending on how your SMTP server is configured. 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. It also could be configured at the config file (web.config, app.config).

  2. No, your application runs under .net and outlook does not affect it.

Upvotes: 2

Dhanashree B
Dhanashree B

Reputation: 1

  1. It depends on the SMTP configuration, and maybe because u haven't configured, you're getting errors. 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.

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("[email protected]");
                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());
            }
 

Upvotes: 0

Related Questions