Reputation: 41
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);
Upvotes: 0
Views: 349
Reputation: 38638
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).
No, your application runs under .net and outlook does not affect it.
Upvotes: 2
Reputation: 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!
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