Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

Here i'm facing prob with sent mail, when i use this script for gmail, working fine but when i try my mail server showing error but in my mail server working fine as sent directly


Here My Script

C#
using (MailMessage mm = new MailMessage(txtemail.Text, "mailme@myserver.com"))
            {
                mm.Subject = "Message From Website NAME : "+txtname.Text;
                mm.Body = "NAME : "+txtname.Text + "\n"+"Address : "+ txtaddr.Text + "\n" +"Phone : "+ txtphone.Text + "\n" +"E-mailID : "+ txtemail.Text + "\n" +"Note : "+ txtnote.Text + "\n";
                mm.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = " mail.myserver.com";
                smtp.EnableSsl = false;
                NetworkCredential NetworkCred = new NetworkCredential("mailme@myserver.com", "pwd");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 26;
                smtp.Send(mm);
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert('E-Mail Sent Successfully');", true);
                txtname.Text = ""; txtaddr.Text = ""; txtemail.Text = ""; txtnote.Text = ""; txtphone.Text = "";
            }


showing error like "Failure sending mail."

how to slove this

thanks..
Posted
Comments
Afzaal Ahmad Zeeshan 26-Mar-15 15:47pm    
What is shown in front of this Failure sending mail message?
Richard Deeming 26-Mar-15 16:24pm    
Is your mail server listening on port 26? That's not the standard SMTP port.
Sinisa Hajnal 27-Mar-15 8:26am    
If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server. If the UseDefaultCredentials property is set to false and the Credentials property has not been set, then mail is sent to the server anonymously.

Since you set UseDefaultCredentials = True, I think your Credentials object is ignored.

Hi,

You can use the sample code given below. Its a working one.

C#
using System.Net.Mail;

    string MailSrvr = "yourserver.com";    
    SmtpClient mclient = new SmtpClient();
    MailMessage mailmsg = new MailMessage();
    mclient.Host = MailSrvr;
    MailAddress frmadd, toadd, tocc, tobcc;

    frmadd = new MailAddress("fromaddress", "From Sender Name");
    mailmsg.From = frmadd;
    mailmsg.IsBodyHtml = true;
    mailmsg.Priority = MailPriority.Normal;
    mailmsg.Subject = "Test Subject";

    strline = "body of the mail";
    mailmsg.Body = strline;

    toadd = new MailAddress("toaddress", "To Receiver Name");
    mailmsg.To.Add(toadd);

    tocc = new MailAddress("ccaddress", "CC");
    mailmsg.CC.Add(tocc);    

    tobcc = new MailAddress("bccaddress", "BCC");
    mailmsg.Bcc.Add(tobcc );    

    mclient.Send(mailmsg); 
    mailmsg.Dispose();


Regards,
Fathima Nishana Jabbar.
 
Share this answer
 
v2
below is the code I have used for emailing purpose I'am sure it will work

Below is the library file you need to use for this purpose
System.Net.Mail;

before going to code in web.config under configuration section
define host i.e. from which you want to send the message.
every site will have port number i have taken gmail as an example its port number is 587

XML
<system.net>
    <mailSettings>
      <smtp from="emailid">
        <network host="smtp@gmail.com" port="587" userName="username" password="password" />
      </smtp>
    </mailSettings>
  </system.net>


below is the actual code to send mail

string toemail = "email address";
        string message = "Hi this is a sample email";
        MailMessage mail = new MailMessage();
        mail.To.Add(toemail);
        mail.Subject = "Sample mail";
        mail.Body = message;
        mail.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.EnableSsl = true;
        smtp.Send(mail);
 
Share this answer
 
v2
Hi Prasanna,

Does your ISP block port 25? Have you try to use port 587? Or, please refer to this post http://dotnet4hosting.asphostportal.com/post/ASPNET-4-Hosting-ASPHostPortal-How-to-Send-Email-with-Gmail-SMTP.aspx[^].

Hope this helps
 
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