Click here to Skip to main content
15,912,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
problem to sending mail showing massage is failure sending mail

What I have tried:

MailMessage MyMail = new MailMessage(SendFrom, SendTo);
if (SendTo.Trim().Length == 0)
throw new Exception("Invalid receiver address.");

MyMail.IsBodyHtml = true;
MyMail.Subject = Subject.Trim();
MyMail.Body = Body.Trim();
System.Net.Mail.Attachment objatt = new Attachment(Attachment);

MyMail.Attachments.Add(objatt);

// Making Credential
//////System.Net.NetworkCredential mailAuthentication = new
//////System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser1"], ConfigurationManager.AppSettings["smtpPassword1"]);
//////System.Net.Mail.SmtpClient smtpHost = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["smtp"], (Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"])));
System.Net.NetworkCredential mailAuthentication = new
System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPassword"]);
System.Net.Mail.SmtpClient smtpHost = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["smtp"], (Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"])));

// Enabling SSL
smtpHost.EnableSsl = true;
smtpHost.UseDefaultCredentials = false;
smtpHost.Credentials = mailAuthentication;
smtpHost.Timeout = 200000;
smtpHost.Send(MyMail);
this code using in dll.
---------------------------------------------------------------
<add key="XmlDir" value="XML">
<add key="smtpUser" value="support@pointindia.co.in">
<add key="smtpPassword" value="password">
<add key="smtp" value="smtp.gmail.com">
<add key="smtpPort" value="465">
<add key="CrystalImageCleaner-AutoStart" value="true">
<add key="CrystalImageCleaner-Sleep" value="60000">
<add key="CrystalImageCleaner-Age" value="120000">

this code usimg in web.config
-----------------------------------------------------------------
and error massage show Failure sending mail.
Posted
Updated 26-Feb-16 16:24pm
Comments
jame01 26-Feb-16 15:43pm    
change this
smtpHost.UseDefaultCredentials = true;
vipan.net 27-Feb-16 0:18am    
Is SSL is Enable for website or you facing issue on local host ?

1 solution

Use this C# class -

C#
   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Data.OleDb;


namespace _1079541_Problem_to_sending_mail_showing_massage_
{
    class Mail
    {
        public static string Profile;
        public static string FromEmail;
        public static string FromName;
        public static string FromPassword;
        public static string SMTP_Host;
        public static int SMTP_Port;
        public static Boolean SMTP_EnableSSL;
        public static string Subject;
        public static string EmailBody;

        public static string SendMail(string mailToEmail, string mailToName, string attachments)
        {
            //LoadMailSetting
            Profile = "Gmail Mail";
            FromEmail = "EmailFrom@GMAIL.COM";
            FromName = "Test Mail";
            FromPassword = "MyPassword";
            SMTP_Host = "smtp.gmail.com";
            SMTP_Port = 587;
            SMTP_EnableSSL = Boolean.Parse("True"); ;
            Subject = "Test Email";
            EmailBody = "Test Email";

            Attachment attachment = new Attachment(attachments.ToString()); //create the attachment
            try
            {

                MailMessage mail = new MailMessage();
                // set the addresses
                mail.From = new MailAddress(FromEmail.ToString());

                mail.To.Add(new MailAddress(mailToEmail.ToString()));

                // set the content
                mail.Subject = Subject.ToString();
                mail.Body = EmailBody.ToString();
                SmtpClient smtp = new SmtpClient(SMTP_Host.ToString(), SMTP_Port);
                smtp.Credentials = new NetworkCredential(FromEmail.ToString(), FromPassword.ToString());
                smtp.EnableSsl = true;
                mail.Attachments.Add(attachment);
                smtp.Send(mail);
                //MessageBox.Show("Mail Sent");
                return "";
            }
            catch (Exception ex)
            {

                return ex.Message.ToString(); // Profile.ToString() & "-" & FromEmail & "_" & FromName & "_" & FromPassword & "_" & SMTP_Host & "_" & SMTP_Port & "_" & SMTP_EnableSSL & "_" & Subject & "_" & EmailBody;
            }
        }
    }
}


You can use this class as below - Call the send mail method
C#
string msg;
msg=Mail.SendMail("ToEmail@yahoo.co.in", "RDBurmon", @"E:\File.xlsx");



VERY important-
Please note that there are substantial changes in gmail account security so as per the changes, you have to follow blow steps in order to send mail from out side gmail portal

1. Login to your gmail account.
2. Visit this page https://accounts.google.com/DisplayUnlockCaptcha and click on button to allow access.
3. Visit this page https://www.google.com/settings/security/lesssecureapps and enable access for less secure apps.
 
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