Send Mail: All about

Windows Server

  • Develop a Windows Service on SQL Server
  • This service will check the MAIL_QUEUE table every a certain of time to send emails
  • Mail sent status will be saved to MAIL_LOG table

C# – How to send email with attachment?

  • Convert String content into Binary data
  • [js]
    byte[] binContent = UtilityHelper.GetByteFromString(strContent);

    public static byte[] GetByteFromString(string str_in)
    {
    if (IsNullOrTrimEmpty(str_in))
    {
    return new byte[0];
    }

    using (MemoryStream memStream = new MemoryStream())
    using (StreamWriter streamWriter = new StreamWriter(memStream))
    {
    streamWriter.Write(str_in);
    streamWriter.Flush();
    return memStream.ToArray();
    }
    }
    [/js]

  • Send mail
  • [js]
    public static string sendAttachmentEmail_DBBinary(string from, string reply, string subject, string to, string Cc,
    string body, string emailPriority, string displayName, string fileName, byte[] myData )
    {
    string msg = “”;
    try
    {
    System.Net.Mail.SmtpClient emailClient = new System.Net.Mail.SmtpClient(AppSettings.GetAppSetting(“SmtpServer”));

    System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
    Message.From = new System.Net.Mail.MailAddress(from, displayName);

    string[] toAddresses = to.Split(‘,’);

    if (toAddresses == null || toAddresses.Length == 0 || UtilityHelper.IsNullOrTrimEmpty(toAddresses[0]))
    {
    return msg = “Error: Email failed to send, no valid email addresses. “;
    }

    for (int i = 0; i < toAddresses.Length; i++) { if (!UtilityHelper.IsNullOrTrimEmpty(toAddresses[i])) { Message.To.Add(new System.Net.Mail.MailAddress(toAddresses[i].Trim())); } } Message.Subject = subject; string[] ccAddresses = Cc.Split(','); if (ccAddresses != null && ccAddresses.Length > 0)
    {
    for (int i = 0; i < ccAddresses.Length; i++) { if (!UtilityHelper.IsNullOrTrimEmpty(ccAddresses[i])) { Message.CC.Add(new MailAddress(ccAddresses[i].Trim())); } } } if (reply.Trim() != "") Message.Headers.Add("Reply-To", reply); switch (emailPriority) { case "Urgent": Message.Priority = System.Net.Mail.MailPriority.High; break; case "Warning": Message.Priority = System.Net.Mail.MailPriority.Normal; break; default: Message.Priority = System.Net.Mail.MailPriority.Low; break; } Message.Body = body; // add the attachment from a stream System.IO.MemoryStream memStream = new System.IO.MemoryStream(myData); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(memStream); streamWriter.Flush(); // this is quite important memStream.Position = 0; System.Net.Mail.Attachment thisAttachment = new System.Net.Mail.Attachment(memStream, System.Net.Mime.MediaTypeNames.Application.Octet); thisAttachment.ContentDisposition.FileName = fileName; Message.Attachments.Add(thisAttachment); emailClient.Send(Message); msg = "Email successfully sent. "; } catch (Exception ex) { msg = "An error occurred the email was not sent."; } return msg; } [/js]

    Error: Can’t not send the emails

    Error messasge

    [html]
    Mailbox unavailable. The server response was: 5.7.1 Unable to relay
    [/html]

    Solution

    https://stackoverflow.com/questions/15717743/mailbox-unavailable-the-server-response-was-5-7-1-unable-to-relay-error

    Be the first to comment

    Leave a Reply

    Your email address will not be published.


    *