my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("mymail#gmail.com", "mypassword");
client.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("mymail#gmail.com", "SMTP Test");
mail.To.Add(new MailAddress("mymail#gmail.com"));
mail.Subject = "Subject test";
mail.Body = "Body test";
client.Send(mail);
MessageBox.Show("mail send.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I get this error:
I cant upload images yet so i use puush.
http://puu.sh/fpAnF/d4372b0b55.png
Error a bit translated:
Error when sending e-mail. system.net.mail.smtpexception: error when
sending e-mail. ---> system.net.webexception: can't connect to the
external server ---> system.net.sockets.sochetexception: can't connect
because the goalcomputer (dont know the exact translation of that) the
connection actively has refused.
can somobody please help me?
Related
I am trying to send an email through a one.com mail server. With receiving one I have no problems.
however with sending I keep getting an SMTP Exception.
System.ApplicationException: 'SmtpException has occured: "failure sending mail"'
UPDATE: after a tip in the comments here is the inner Exception
WebException: Unable to connect to the remote server
This is my code
public void email_send(string sendTo)
{
try
{
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(sendTo));
msg.From = new MailAddress("myMail#mydomein.be", "my name");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("myMail#mydomein.be", "myPassword");
client.Port = 587;
client.Host = "mailout.one.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Send(msg);
Console.WriteLine("email was sent successfully!");
}
catch (SmtpException ex)
{
throw new ApplicationException
("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
throw ex;
}
}
I also have the following information form my email provider (one.com):
As discussed in the comments, it looks like you can only use mailout.one.com from One.com-hosted websites - it refuses connections from elsewhere.
Instead you need to use send.one.com from other connections, which supports the same set of ports.
I am trying to send mail from Web API using SMTP (GODaddy). It failed to send the mail and returning the exception as follows
Code :
public void SendMail()
{
try
{
MailMessage mail = new MailMessage("support#abc.com","toMail");
mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "this is email body";
SmtpClient client = new SmtpClient("relay-hosting.secureserver.net");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("support#abc.com", "****");
client.Port = 25;
client.Send(mail);
}
catch (SmtpFailedRecipientException Ex)
{
Ex.FailedRecipient.ToString();
}
}
Can anyone help me to fix this.
I am sending mail from c# but it produces "Failure Sending mail". I have enabled less secured option but still produces error. I have checked the help on internet but not working. Please give proper solution.
Below is my code:
private void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smptServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mama.bannu123#gmail.com");
mail.To.Add(Convert.ToString(this.txtToMailAddress.Text));
mail.Subject=Convert.ToString(this.txtSubjectMail.Text);
mail.Body=Convert.ToString(this.txtBodyMail.Text);
System.Net.Mail.Attachment attachFile = new Attachment("D:\\exp1.pdf");
mail.Attachments.Add(attachFile);
smptServer.Port = 587;
smptServer.Credentials = new
System.Net.NetworkCredential("mama.bannu123#gmail.com", "password code");
smptServer.EnableSsl = true;
smptServer.Send(mail);
MessageBox.Show("Message Send Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I'm trying to figure out how to create a simple contact page in order to send an email. Right now I'm just testing but i'm not getting any error message.
Here is my code:
protected void btnSend_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.To.Add(txtFrom.Text);
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
SmtpClient smtp = new SmtpClient("localhost");
smtp.Send(msg);
Response.Write("Your email was sent");
}
Do you have an Smtp service running on your localhost? If not you can use Gmail's smtp service for testing.
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential("<your gmail login>", "<your gmail password>");
client.EnableSsl = true;
As others have pointed out, you probably don't have the localhost as a real mail server. To get a better idea, turn Tracing on and change your code to something more like this:
protected void btnSend_Click(object sender, EventArgs e) {
Trace.Write("btnSend_Click initialized");
string resp = "An unknown error occured.";
using (MailMessage msg = new MailMessage()) {
try {
msg.To.Add(txtFrom.Text);
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
SmtpClient smtp = new SmtpClient("localhost");
Trace.Write("smtp client created.");
smtp.Send(msg);
Trace.Write("smtp message sent.");
resp = "Your message was sent.";
} catch (Exception ex) {
Trace.Warn("Smtp Error", ex.Message);
resp = "There was an error sending your message.";
}
}
Response.Write(resp);
Trace.Write("btnSend_Click completed");
}
You must specify a From address like this:
msg.From = new MailAddress("noreply#noreply"); // Use a real address, of course
Because you are not getting an exception, I wonder if btnSend_Click is getting hit at all...
Other things to look at:
Is your txtFrom.Text being overwritten on postback? You might hardcode your email address during debugging.
Try 127.0.0.1 instead of localhost.
I'm using the following basic code:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.to.add("someone#hotmail.com");
msg.to.add("someone#gmail.com");
msg.to.add("someone#myDomain.com");
msg.From = new MailAddress("me#myDomain.com", "myDomain", System.Text.Encoding.UTF8);
msg.Subject = "subject";
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "body";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
//Add the Creddentials
SmtpClient client = new SmtpClient();
client.Host = "192.168.0.24";
client.Credentials = new System.Net.NetworkCredential("me#myDomain.com", "password");
client.Port = 25;
try
{
client.Send(msg);
}
catch (System.Net.Mail.SmtpException ex)
{
sw.WriteLine(string.Format("ERROR MAIL: {0}. Inner exception: {1}", ex.Message, ex.InnerException.Message));
}
Problem is the mail is only sent to the address in my domain (someone#mydomain.com) and I get the following exception for the 2 other addresses:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: No such domain at this location
I suspect it's something to do with something blocking my smtp client but not sure how to approach this.
Any idea? thanks!
Ron is correct,just use the 587 port and it will work as u wish.
Check this code and see if it works:
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address#mfc.ae");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Try using port=587. Here's useful related link: http://mostlygeek.com/tech/smtp-on-port-587/comment-page-1/