Sending an email using c# - c#

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.

Related

Using proxy when sending email. C#

I have a program which is sending emails to my clients. The thing i wanna do is to hide the ip from which the email was send. Is this even possible if yes how? This is my code:
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
MailMessage mail = new MailMessage(email, target, title, msg);
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential(email, password);
try
{
smtpClient.Send(mail);
}
catch (Exception e) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine($"Error occured while trying to send email:\n{e.Message}"); Console.ResetColor(); }
You're looking at the wrong side. Gmail already strips the IP address of the client. If you own an email server this can be configured as well.

How can i send email from/to any emails addresses like yahoo gmail pop3?

What i tried now is to send email to my own gmail account but what ever i tried so far i'm getting error when trying to send.
I didn't try to send yet to my pop3 email and not to yahoo or from my pop3 or from yahoo. For now i'm trying to send to my gmail.
In a button click event:
private void button1_Click(object sender, EventArgs e)
{
sendit("myg#gmail.com");
}
Then in the method sendit
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("myg#gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("myg#gmail.com", "myp");
client.EnableSsl = true;
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
Using break point it's always getting to the catch part show the message:
"The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required.
Learn more at"
Remove client.UseDefaultCredentials = true; and try again!
If UseDefaultCredentials is set, the client tries to send the E-Mail with the currently logged on user. If UseDefaultCredentials is false, then it is going to use your manually set Credentials!
It is a security issue, Gmail by default prevents access for your mail account from custom applications. You need to turn on the access for less secure applications.
Here is the link: Less secure applications

SmtpException was caught when sending mail

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?

sending email is not working with my smtp server

The following is my coding for sending email with my smtp server. but it throws an smtpexception as failure to sent .
I am using windows webserver for my website.
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add("info#dhuvara.com");
Msg.Subject = txtSubject.Text;
Msg.Body = txtMessage.Text;
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.dhuvara.com";
smtp.Port = 25;
Msg.Priority = MailPriority.Normal;
smtp.Credentials = new System.Net.NetworkCredential("info#dhuvara.com", "SolaiMail");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Thanks for Contact us";
// Clear the textbox valuess
txtName.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
You need to add smtp.UseDefaultCredentials = false;
It needs to be before you set the credentials.
Try to set EnableSSL = false;
It might be the problem - i've run into this problem before.
I doubt that mail.dhuvara.com is the right host to connect to.
DNS lookup for this host tells me, that it is a CNAME for go.domains.live.com and go.domains.live.com is a CNAME for domains.live.com, so I assume that you are using windows live to host your domain. Maybe you should try smtp.live.com as the mail server instead.

Can't send smtp email from network using C#, asp.net website

I have my code here, it works fine from my home, where my user is administrator, and I am connected to internet via a cable network.
But, problem is when I try this code from my work place, it does not work. Shows error:
"unable to connect to the remote server"
From a different machine in the same network:
"A socket operation was attempted to an unreachable network 209.xxx.xx.52:25"
I checked with our network admin, and he assured me that all the mail ports are open [25,110, and other ports for gmail].
Then, I logged in with administrative privilege, there was a little improvement, it did not show any error, but the actual email was never received.
Please note that, the code was tested from development environment, visual studio 2005 and 2008.
Any suggestion will be much appreciated.
Thanks in advance
try
{
MailMessage mail_message = new MailMessage("xxxxx#y7mail.com", txtToEmail.Text, txtSubject.Text, txtBody.Text);
SmtpClient mail_client = new SmtpClient("SMTP.y7mail.com");
NetworkCredential Authentic = new NetworkCredential("xxxxx#y7mail.com", "xxxxx");
mail_client.UseDefaultCredentials = true;
mail_client.Credentials = Authentic;
mail_message.IsBodyHtml = true;
mail_message.Priority = MailPriority.High;
try
{
mail_client.Send(mail_message);
lblStatus.Text = "Mail Sent Successfully";
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
lblStatus.Text = "Mail Sending Failed\r\n" + ex.Message;
}
}
catch (Exception ex)
{
lblStatus.Text = "Mail Sending Failed\r\n" + ex.Message;
}
Here is some sample code that works for me and talks to a gmail server
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
Try setting UseDefaultCredentials to false.
Also try switching SSL on for y7mail
mail_client.EnableSSL = true;
Source - http://help.yahoo.com/l/au/yahoo7/edit/registration/registration-488246.html
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
This is the best answer for System.Exception Problem. Totally verified, you need to provide username and password at network credentials. Also try to give this solution to other sites.

Categories

Resources