Hi @Apex6554,
It seems that your question is not related to MS SQL Server. Please remove that tag from your question.
If your question is related to c#, please try it like below.
All static data is stored in the app.config file.
void Main()
{
try
{
string smtpAddress = AppSettings.Get<string>("smtpAddress"); //ConfigurationManager.AppSettings["smtpAddress"];
int portNumber = AppSettings.Get<int>("portNumber"); //ConfigurationManager.AppSettings["portNumber"];
bool enableSSL = AppSettings.Get<bool>("enableSSL"); ;
bool UseDefaultCredentials = AppSettings.Get<bool>("UseDefaultCredentials"); ;
string emailFrom = ConfigurationManager.AppSettings["emailFrom"];
string password = ConfigurationManager.AppSettings["password"];
string emailTo = ConfigurationManager.AppSettings["emailTo"];
string subject = ConfigurationManager.AppSettings["subject"];
string body = ConfigurationManager.AppSettings["body"];
StringBuilder sb = new StringBuilder();
sb.AppendLine(body);
sb.AppendLine(string.Format("SMTP server: {0}, port: {1}", smtpAddress, portNumber));
sb.AppendLine(string.Format("Sent from {0} machine on: {1}.", Environment.MachineName, DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz")));
sb.AppendLine(string.Format(".Net version: {0}", Environment.Version));
sb.AppendLine(string.Format("OS version: {0}", Environment.OSVersion.VersionString));
body = sb.ToString();
Console.WriteLine("SMTP server: {0}, port: {1}", smtpAddress, portNumber);
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
mail.Priority = MailPriority.High;
// mail.Attachments.Add(new Attachment(@"C:\SomeFile.txt"));
// mail.Attachments.Add(new Attachment(@"C:\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
if (UseDefaultCredentials)
{
smtp.UseDefaultCredentials = true;
//smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
}
else
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
}
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Console.WriteLine("An e-mail has been successfully sent to: {0}.", emailTo);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// Define other methods and classes here
public static class AppSettings
{
public static T Get<T>(string key)
{
var appSetting = ConfigurationManager.AppSettings[key];
if (string.IsNullOrWhiteSpace(appSetting)) throw new SettingsPropertyNotFoundException(key);
var converter = TypeDescriptor.GetConverter(typeof(T));
return (T)(converter.ConvertFromInvariantString(appSetting));
}
}
<configuration>
<appSettings>
<add key="smtpAddress" value="smtp.mail.yahoo.com"/>
<add key="portNumber" value="587"/>
<add key="enableSSL" value="true"/>
<add key="emailFrom" value="ykhabins@yahoo.com"/>
<add key="password" value="Password"/>
<add key="UseDefaultCredentials" value="false"/>
<add key="emailTo" value="ykhabins@gmail.com"/>
<add key="subject" value="Hello"/>
<add key="body" value="This is e-mail testing via SMTP protocol."/>
</appSettings>
</configuration>