Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a console application to send email. I will use company's email server. It needs the user's network id. I don't know whether I have the Windows login password ready.

The difficult thing is how can I get my password?
C#
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

...
try
{

   SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
   System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("username", "password");
   mySmtpClient.Credentials = basicAuthenticationInfo;

   // add from,to mailaddresses
   MailAddress from = new MailAddress("test@example.com", "TestFromName");
   MailAddress to = new MailAddress("test2@example.com", "TestToName");
   MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

   // add ReplyTo
   MailAddress replyto = new MailAddress("reply@example.com");
   myMail.ReplyToList.Add(replyTo);

   // set subject and encoding
   myMail.Subject = "Test message";
   myMail.SubjectEncoding = System.Text.Encoding.UTF8;


What I have tried:

In my code I hard coded the username and password. But the application is for any user. It shouldn't put it in hard code.
C#
System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("username", "password");
Posted
Updated 5-Nov-19 11:04am

You have a couple of options....

Your application can ask for user name and password and populate those portions of the script appropriately.

You could also use the current logged in users credentials
CredentialCache.DefaultCredentials Property (System.Net) | Microsoft Docs[^]

If you need to know who is using the application (logging purposes), then you can retrieve it with System.Security.Principal.WindowsIdentity.GetCurrent()
WindowsIdentity Class (System.Security.Principal) | Microsoft Docs[^]
 
Share this answer
 
You can't get your password because Windows doesn't store it. If the smtp server is using the actual windows account then use Windows Authentication, google smtp with windows authentication for more details.

Alternatively smtp servers will have a single username and password you use in your code and the smtp server has mail relaying enabled so you can send "from" any account through that single set of credentials.

The solution all depends on how your server and network are configured.
 
Share this answer
 
///----------------------------------------------------------------------------------------
/// <summary>
/// Send Email to a List of Recipients
/// </summary>
/// 
private void Deliver(List<Deliver> deliver)
{

	try
	{
		//Get Configuration from App.config
		string email = ConfigurationManager.AppSettings["email"].ToString();
		string emailpass = ConfigurationManager.AppSettings["emailpass"].ToString();
		string host = ConfigurationManager.AppSettings["host"].ToString();
		string port = ConfigurationManager.AppSettings["port"].ToString();
		string SSL = ConfigurationManager.AppSettings["SSL"].ToString();
		string Credentials = ConfigurationManager.AppSettings["Credentials"].ToString();
		string nick = ConfigurationManager.AppSettings["Nick"].ToString();

		foreach (var item in deliver)
		{
			// Mail Obejct
			MailMessage mail = new MailMessage();
			mail.From = new MailAddress(email, nick);

		   
			mail.To.Add(item.Email);
			mail.Subject = item.Subject;

			// Body.
			string text = Body;
			AlternateView plainView = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);


			// HTML Body ...
			string html = item.Body + text + " <img src='cid:imagen' />";
			AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, MediaTypeNames.Text.Html);

			// Image to put inside.
			LinkedResource img = new LinkedResource(Pic, MediaTypeNames.Image.Jpeg);
			img.ContentId = "imagen";

			// Into HTML ...
			htmlView.LinkedResources.Add(img);

			// Put Toguether ...
			mail.AlternateViews.Add(plainView);
			mail.AlternateViews.Add(htmlView);
			mail.IsBodyHtml = true;

			// Send via SMTP server ...
			SmtpClient client = new SmtpClient();
			client.Port = int.Parse(port);
			client.Host = host;
			client.EnableSsl = Boolean.Parse(SSL);
			client.Timeout = 10000;
			client.DeliveryMethod = SmtpDeliveryMethod.Network;
			client.UseDefaultCredentials = Boolean.Parse(Credentials);
			client.Credentials = new System.Net.NetworkCredential(email, emailpass);
			client.Send(mail);
			text = string.Empty;
			html = string.Empty;
			img.Dispose();
		}
	}
	catch (SmtpException ex)
	{

			return;
	}


}
 
Share this answer
 
///----------------------------------------------------------------------------------------
///
/// Send Email to a List of Recipients
///

///
private void Deliver(List<deliver> deliver)
{

try
{
//Get Configuration from App.config
string email = ConfigurationManager.AppSettings["email"].ToString();
string emailpass = ConfigurationManager.AppSettings["emailpass"].ToString();
string host = ConfigurationManager.AppSettings["host"].ToString();
string port = ConfigurationManager.AppSettings["port"].ToString();
string SSL = ConfigurationManager.AppSettings["SSL"].ToString();
string Credentials = ConfigurationManager.AppSettings["Credentials"].ToString();
string nick = ConfigurationManager.AppSettings["Nick"].ToString();

foreach (var item in deliver)
{
// Mail Obejct
MailMessage mail = new MailMessage();
mail.From = new MailAddress(email, nick);


mail.To.Add(item.Email);
mail.Subject = item.Subject;

// Body.
string text = Body;
AlternateView plainView = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);


// HTML Body ...
string html = item.Body + text + " ";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, Encoding.UTF8, MediaTypeNames.Text.Html);

// Image to put inside.
LinkedResource img = new LinkedResource(Pic, MediaTypeNames.Image.Jpeg);
img.ContentId = "imagen";

// Into HTML ...
htmlView.LinkedResources.Add(img);

// Put Toguether ...
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
mail.IsBodyHtml = true;

// Send via SMTP server ...
SmtpClient client = new SmtpClient();
client.Port = int.Parse(port);
client.Host = host;
client.EnableSsl = Boolean.Parse(SSL);
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = Boolean.Parse(Credentials);
client.Credentials = new System.Net.NetworkCredential(email, emailpass);
client.Send(mail);
text = string.Empty;
html = string.Empty;
img.Dispose();
}
}
catch (SmtpException ex)
{

return;
}


}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900