0

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?
Airn5475
  • 2,452
  • 29
  • 51
Jay
  • 41
  • 7
  • Some settings can be configured in the web.config. [Related question](https://stackoverflow.com/questions/19233108/how-to-configure-smtp-settings-in-web-config) – Cleptus Aug 12 '20 at 12:33
  • Note: The SMTP server could be configured to allow relay (non autenticated email) but most likely the settings are in the web.config file – Cleptus Aug 12 '20 at 12:36
  • @Cleptus Does that mean if my outlook account is configured as admin@outlook.com and the from mail address is user@outlook.com, will it still work? Can i send mails from any email ids without the user name and password of the corresponding user? – Jay Aug 12 '20 at 12:38
  • If and only if the SMTP server is configured to allow it. Not an usual scenario, but if it allows relay, you could authenticate with some credentials and send from a different address – Cleptus Aug 12 '20 at 12:39
  • 1
    Outlook is not necessary to send emails. Mail servers like GMAIL accept the SMTP object that the Net Library produces. GMAIL requires credentials. The default credentials are taking from Control Panel User Mail settings (like a POP account). – jdweng Aug 12 '20 at 12:40
  • @jdweng thanks, my requirement is to change this code to use office 365. So if I pass the credentials from the control panel then no code changes required i believe. Am I right? – Jay Aug 12 '20 at 12:44
  • you should be fine, just make sure the settings do work [How to set up SMTP AUTH client submission](https://learn.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-microsoft-365-or-office-365#how-to-set-up-smtp-auth-client-submission) – Cleptus Aug 12 '20 at 12:48
  • If you are on a network with an outlook server you first have to open Outlook once which will setup an email account on machine. Then once the email account is setup use DefaultCrentials (required since Net 4.0) and your email will automatically get routed to your Outlook Email Server. – jdweng Aug 12 '20 at 12:50
  • @jdweng Where are you getting info that .NET SMTP is somehow tied to Outlook configuration? – mason Aug 12 '20 at 13:25
  • @mason : Because that is the way outlook and SMTP works. – jdweng Aug 12 '20 at 13:29
  • @jdweng We're not dealing with Outlook though. We're dealing with .NET SMTP, which I do not believe uses Outlook settings. I see you saying it does, but that isn't sufficient evidence. Do you have anything that backs your statement up? – mason Aug 12 '20 at 13:44
  • https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.usedefaultcredentials?view=netcore-3.1 – jdweng Aug 12 '20 at 13:46
  • @jdweng And that does not back up your statement at all. You said that you must launch Outlook, which will set up an email account on your machine, and then when you use DefaultCredentials it will automatically use those settings. The documentation on that does not make any indications that's the case. The reality is that DefaultCredentials uses the Windows account that the application is running as, and has no dependency on Outlook being configured on the machine. Please remove your incorrect statements, so that you don't confuse people, and be more considerate in the future. – mason Aug 12 '20 at 13:50
  • Read Remarks carefully. – jdweng Aug 12 '20 at 13:57
  • I have read your remarks carefully. They are not accurate, and you are pushing inaccurate information to people that aren't going to understand this. Can you find any evidence to support your position that .NET SMTP uses settings that are configured when launching Outlook? – mason Aug 12 '20 at 14:03

2 Answers2

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

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • Does that mean if my outlook account is configured as admin@outlook.com and the from mail address is user@outlook.com, will it still work? Who will be the sender of the mail? – Jay Aug 12 '20 at 12:36
  • @Jay go check the web.config file and look for a `mailSettings` section – Cleptus Aug 12 '20 at 12:38
0
  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("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());
            }