I am trying to send email in C# using this code but smtp Exception Failure to send
this is my code
message.From = new MailAddress("[email protected]", "Mark Fenech");
message.To.Add(txt_emailTo.Text);
message.Subject = txt_Subject.Text;
message.Body = txt_emailBody.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "LOCALHOST";
smtp.EnableSsl = false;
smtp.Send(message);
Loading
Jonathas SucupiraPosted Apr 21, 2011, 9:08 AM
I've organized your code so try this:
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
ss.EnableSsl = true;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
try
{
message.From = new MailAddress("[email protected]", "Mark Fenech");
message.To.Add(txt_emailTo.Text);
message.Subject = txt_Subject.Text;
message.Body = txt_emailBody.Text;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtp.Send(message);
}
catch (Exception e)
{
Response.Write("Error: " + e.Message);
}
Sam HobbsPosted Apr 21, 2011, 8:32 PM
Mark FenechPosted Apr 21, 2011, 1:31 PM
Sam HobbsPosted Apr 21, 2011, 12:51 PM
As I said, please "refer to the articles in this web site". Have you looked at the following two?
Mark FenechPosted Apr 21, 2011, 5:09 AM
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = false;
message.From = new MailAddress("[email protected]", "Mark Fenech");
message.To.Add(txt_emailTo.Text);
message.Subject = txt_Subject.Text;
message.Body = txt_emailBody.Text;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtp.Send(message);
Sam HobbsPosted Apr 20, 2011, 5:35 PM
Suthish NairPosted Apr 20, 2011, 5:08 PM
Sam HobbsPosted Apr 20, 2011, 3:27 PM
I doubt that you want to use LOCALHOST for the server. The host should be the name of a server to send the mail. Ankit might be correct.
You should find articles that describe how to send Hotmail. You can probably use your ISP's server to send email, but I am not sure if that will work for Hotmail.
Ankit NandekarPosted Apr 20, 2011, 1:12 PM
we can write this
smtp.Host="smtp.gmail.com";
Full program to send a mail
Design a Form With 2 Textbox txtuserName and txtPassword take one Send Button.
write this code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
MailMessage msgobj;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
SmtpClient serverobj = new SmtpClient();
serverobj.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text);
serverobj.Port = 587;//25 is Default port number for gmail
serverobj.Host = "smtp.gmail.com";
serverobj.EnableSsl = false;
msgobj = new MailMessage();
msgobj.From = new MailAddress(txtUserName.Text,"MessageName");
//write here emailid to whom we r sending mail or pass through text box
msgobj.To.Add("[email protected]");
msgobj.Subject = "MessageDemo";
msgobj.Body = "Write here Message Body or Pass through TextBox";
msgobj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
serverobj.Send(msgobj);
}
}
Jonathas SucupiraPosted Apr 20, 2011, 12:56 PM
type: telnet 127.0.0.1 25
it will tell you if it was able to connect to the smtp server
reference = http://www.webhostingtalk.com/showthread.php?t=440806
Also keep in mind that a lot of the times software firewalls blocks port 25.
Mark FenechPosted Apr 20, 2011, 12:47 PM
Jonathas SucupiraPosted Apr 20, 2011, 12:42 PM
smtp.port = 25;
smtp.UseDefaultCredentials = True;
If that doesn't work maybe you should try to make sure the SMTP server is running correctly.