Click here to Skip to main content
16,012,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am trying to send mail using asp.net and I used the following code and getting the Exception Failed sending mail

C#
<system.net>
    <mailSettings>
      <smtp from="abinav@ymail.com">
        <network host="smtp.bizmail.yahoo.com" port="465" userName="abinav@ymail.com" password="Mypwd" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

 public void sendMail()
    {
        //SmtpClient smtpClient = new SmtpClient();
        //MailMessage message = new MailMessage();
        try
        {
            string toEmailAddress = "mahesh.krishnan@gmail.com";
            string ymailId = "abinav@Ymail.com";
            string bodyMsg = "hello its testing mail";
            string subject = "testing mail";
            MailMessage mail = new MailMessage();
            mail.To.Add(toEmailAddress);
            mail.From = new MailAddress(ymailId);
            mail.Subject = subject;
            mail.Body = bodyMsg;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            //smtp.Timeout = 3000;
            smtp.EnableSsl = true;
            smtp.Send(mail);

        }
        catch (Exception ex)
        {
          throw(ex);
        }
}
Posted

Probably, the problem is that you do not have any authorization in there, so the email system is rejecting your request. Have a look at this: it works! Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
XML
/// <param name="fromAddress">Message from address</param>
/// <param name="fromDisplay">Display name for "message from address"</param>
/// <param name="credentialUser">User whose credentials are used for message send</param>
/// <param name="credentialPassword">User password used for message send</param>
/// <param name="attachments">Optional attachments for message</param>
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)
    {
    string host = ConfigurationManager.AppSettings["SMTPHost"];
    body = UpgradeEmailFormat(body);
    try
        {
        MailMessage mail = new MailMessage();
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress(to));
        mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
        mail.Subject = subject;
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Priority = MailPriority.Normal;
        foreach (MailAttachment ma in attachments)
            {
            mail.Attachments.Add(ma.File);
            }
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
        smtp.Host = host;
        smtp.Send(mail);
        }
    catch (Exception ex)
        {
        StringBuilder sb = new StringBuilder(1024);
        sb.Append("\nTo:" + to);
        sb.Append("\nbody:" + body);
        sb.Append("\nsubject:" + subject);
        sb.Append("\nfromAddress:" + fromAddress);
        sb.Append("\nfromDisplay:" + fromDisplay);
        sb.Append("\ncredentialUser:" + credentialUser);
        sb.Append("\ncredentialPasswordto:" + credentialPassword);
        sb.Append("\nHosting:" + host);
        ErrorLog(sb.ToString(), ex.ToString(), ErrorLogCause.EmailSystem);
        }
    }
"UpgradeEmailFormat" and "ErrorLog" are generic routines; you don't need the former, and can write the later yourself!
"MailAttachment" is a simple class you only need if you want to send attachments:
 Collapse | Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net.Mime;
using System.Net.Mail;
public class MailAttachment
    {
    #region Fields
    private MemoryStream stream;
    private string filename;
    private string mediaType;
    #endregion
    #region Properties
    /// <summary>
    /// Gets the data stream for this attachment
    /// </summary>
    public Stream Data { get { return stream; } }
    /// <summary>
    /// Gets the original filename for this attachment
    /// </summary>
    public string Filename { get { return filename; } }
    /// <summary>
    /// Gets the attachment type: Bytes or String
    /// </summary>
    public string MediaType { get { return mediaType; } }
    /// <summary>
    /// Gets the file for this attachment (as a new attachment)
    /// </summary>
    public Attachment File{ get {return new Attachment(Data, Filename, MediaType); } }
    #endregion
    #region Constructors
    /// <summary>
    /// Construct a mail attachment form a byte array
    /// </summary>
    /// <param name="data">Bytes to attach as a file</param>
    /// <param name="filename">Logical filename for attachment</param>
    public MailAttachment(byte[] data, string filename)
        {
        this.stream = new MemoryStream(data);
        this.filename = filename;
        this.mediaType = MediaTypeNames.Application.Octet;
        }
    /// <summary>
    /// Construct a mail attachment from a string
    /// </summary>
    /// <param name="data">String to attach as a file</param>
    /// <param name="filename">Logical filename for attachment</param>
    public MailAttachment(string data, string filename)
        {
        this.stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(data));
        this.filename = filename;
        this.mediaType = MediaTypeNames.Text.Html;
        }
    #endregion
    }
 
Share this answer
 
MailMessage oMsg = new MailMessage();
                oMsg.From = new MailAddress("xxx@gmail.com", "xxx");
                oMsg.To.Add(new MailAddress("yyy@gmail.com", "xxx"));
                oMsg.Subject = "Packet Parsing Problem";
                oMsg.Body = " Problem Occuread test mail";
                oMsg.SubjectEncoding = System.Text.Encoding.UTF8;
                oMsg.BodyEncoding = System.Text.Encoding.UTF8;
                oMsg.IsBodyHtml = false;
                oMsg.Priority = MailPriority.High;
 
                SmtpClient oSmtp = new SmtpClient("smtp.gmail.com", 587);
                oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                oSmtp.EnableSsl = true;
 
                NetworkCredential oCredential = new NetworkCredential("xxx@gmail.com", "123456aA");
                oSmtp.UseDefaultCredentials = false;
                oSmtp.Credentials = oCredential;
                oSmtp.Send(oMsg);



Try this code , its working for me .
 
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