Saturday, July 27, 2013

Send E-Mail from C# Code

try
            {
                StringBuilder strMailContent = new StringBuilder();
                MailMessage mail = new MailMessage();
                String toName = "tomail@company.net";
                String fromName = "frommail@company.net";
                String strMessage = "Test Message from C# Code";
                String strSMTPAddress = "xxx.xxx.xxx.xxx";
                String strUserName = "domain_forest_name";
                String strPassword = "domain_pwd";
                String strDomain = "domain_name";

                mail.To.Add(toName);
                mail.From = new MailAddress(fromName);
                mail.Subject = "test mail from C# code. " +DateTime.Now.ToString("F"); //("MMM ddd d HH:mm yyyy");
                strMailContent.Append("<html><body><table>");
                strMailContent.Append("<tr><td>Dear " + toName + "</td></tr>");
                strMailContent.Append("</table>");
                strMailContent.Append("</body></html></table>");
                mail.Body = strMailContent.ToString();
                SmtpClient smtp = new SmtpClient(strSMTPAddress);
                smtp.Credentials = new System.Net.NetworkCredential(strUserName, strPassword, strDomain);
                smtp.Send(mail);
            }
            catch (Exception ex) {
                SPDiagnosticsService diagnosticsService = SPDiagnosticsService.Local;
                string errorMsg = ex.Message;
                diagnosticsService.WriteTrace(0, new SPDiagnosticsCategory("AleartSettingTimerJob"TraceSeverity.Monitorable, EventSeverity.Error),TraceSeverity.Monitorable, "Writing to the ULS log: {0}"new object[] { errorMsg });
                diagnosticsService.WriteEvent(0, new SPDiagnosticsCategory("AleartSettingTimerJob"TraceSeverity.Monitorable, EventSeverity.Error),EventSeverity.Error, "Writing to the Event Viewer log: {0}"new object[] { errorMsg });
            }

1 comment:

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   &qu...

Popular posts