locked
SMTP failure sending mail RRS feed

  • Question

  • Hi,

    I'm using the below code to send email by downloading documents from sharepoint. I'm receiving the error "Failure sending mail. at System.Net.Mail.SmtpClient.Send(MailMessage message)" during smtp.Send(mailMessage);

    using (MailMessage mailMessage = new MailMessage())
                    {
                        mailMessage.Subject = "Subject Title";
                        mailMessage.Body = "Body message";
                        mailMessage.IsBodyHtml = true;
                        mailMessage.To.Add("someemail@email.com");
                        foreach (ListItem item in listItems)
                        {
                            string fileRef = item["FileRef"].ToString();
                            string fileName = item["FileLeafRef"].ToString();
                            using (FileInformation spFileInfo = File.OpenBinaryDirect(context, fileRef))
                            {
                                mailMessage.Attachments.Add(new Attachment(spFileInfo.Stream, fileName));
                            }
                        }
    
                        SmtpClient smtp = new SmtpClient();
                        using (smtp as IDisposable)
                        {
                            smtp.Send(mailMessage);
                            isMailSent = true;
                        }
                    }

    How to resolve the error while sending sharepoint documents through smtp?

    Monday, October 29, 2012 2:54 AM

Answers

  • foreach (ListItem item in items)
                                    {
                                        string fileRef = item["FileRef"] != null ? item["FileRef"].ToString() : string.Empty;
                                        string fileName = item["FileLeafRef"].ToString();
                                        string tempFilePath = System.IO.Path.Combine(tempDirectory, fileName);
    
                                        using (FileInformation spFileInfo = File.OpenBinaryDirect(context, fileRef))
                                        {
                                            byte[] bytearr = ReadFully(spFileInfo.Stream);
                                            System.IO.File.WriteAllBytes(tempFilePath, bytearr);
                                            mailMessage.Attachments.Add(new Attachment(tempFilePath));
                                        }
                                    }
    
    public byte[] ReadFully(System.IO.Stream input)
            {
                byte[] buffer = new byte[16 * 1024];
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    int read;
                    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    return ms.ToArray();
                }
            }


    Anandhan.S Remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, October 31, 2012 6:14 AM

All replies

  • Smtp.Host = "*.*.*.*";
    Smtp.Credentails = new NetworkCredentials(username,password);

    You will have to define the host server (IP Address) and crendentials for the smtp server.

     


    Thanks, Bhadr :) Mark as Answer if it solves your problem. Vote As Helpful if the post is helpful for you.

    Monday, October 29, 2012 3:41 AM
  • 1. Configure the outbound SMTP server in Central Administration -> System Settings -> outbound email

    2. Use Microsoft.SharePoint.Utilities.SPUtility.SendEmail(... to send the mail message instead of SmtpClient.Send.


    My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction

    Monday, October 29, 2012 5:32 AM
  • Hi,

    I'm using the SharePoint 2010 COM to send email from WCF service. I already configured mail settings in web.config. WCF hosted machine IP address is added in the mail server for relay.

    <system.net>    
        <mailSettings>
          <smtp deliveryMethod="Network" from="noreply@email.com">
            <network host="mail.domain.local" port="25"/>
          </smtp>
        </mailSettings>
      </system.net>

    mail is working fine without sharepoint document attachment. problem occurs during sharepoint document attachment in mail,


    Anandhan.S Remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Monday, October 29, 2012 5:44 AM
  • foreach (ListItem item in items)
                                    {
                                        string fileRef = item["FileRef"] != null ? item["FileRef"].ToString() : string.Empty;
                                        string fileName = item["FileLeafRef"].ToString();
                                        string tempFilePath = System.IO.Path.Combine(tempDirectory, fileName);
    
                                        using (FileInformation spFileInfo = File.OpenBinaryDirect(context, fileRef))
                                        {
                                            byte[] bytearr = ReadFully(spFileInfo.Stream);
                                            System.IO.File.WriteAllBytes(tempFilePath, bytearr);
                                            mailMessage.Attachments.Add(new Attachment(tempFilePath));
                                        }
                                    }
    
    public byte[] ReadFully(System.IO.Stream input)
            {
                byte[] buffer = new byte[16 * 1024];
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    int read;
                    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    return ms.ToArray();
                }
            }


    Anandhan.S Remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, October 31, 2012 6:14 AM