I want to send mail from my website domain(www.sample.com)
I have written below code. It is not returning any error. But it is not sending mail once I uploaded the pages in the server. Once click on email send, it is not getting any error, but not receiving the mail.
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("mail.sample.co.uk", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("smtpuser#sample.co.uk", "pass#123");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//Setting From , To and CC
mail.From = new System.Net.Mail.MailAddress("smtpuser#sample.co.uk", "MyWeb Site");
mail.To.Add(new System.Net.Mail.MailAddress("myemail#gmail.com"));
mail.CC.Add(new System.Net.Mail.MailAddress("myemail1#gmail.com"));
smtpClient.Send(mail);
I am getting the "mailsend" response text in the page once calling the sending method.
Please help
Anjana
The following code works perfect for me. Make sure you have added a mail account on the smtp server and activated it.
private static string EMAIL_SERVER = "mailservername.mydomain.com";
private static string EMAIL_NAME = "myMailAccount#mydomain.com";
private static string EMAIL_PASSWORD = "complex_password123";
private static string EMAIL_SEND_TO = "sample#mail.com";
protected string SendMail()
{
try
{
System.Net.Mail.MailMessage objMM = new System.Net.Mail.MailMessage();
objMM.From = new MailAddress("fromMe#mail.com", "john doe");
objMM.To.Add(new MailAddress(EMAIL_SEND_TO)); //Note: this To a collection
objMM.Subject = "Subject1";
objMM.Body = "Hello world this is my text";
objMM.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(EMAIL_SERVER);
smtp.Credentials = new NetworkCredential(EMAIL_NAME, EMAIL_PASSWORD);
smtp.Send(objMM);
}
catch (Exception e)
{
return "Message can not be send couse of error: " + e.ToString();
}
return "Message is send.";
}
Related
I am trying to send mail using GoDaddy mail server with SMTPClient in ASP.NET and my code is below , I have tried all the ports in GoDaddy but i couldn't send a mail
My code:
try
{
//Mail Message
MailMessage mM = new MailMessage();
//Mail Address
mM.From = new MailAddress("xxx#sender.com");
//receiver email id
mM.To.Add("xxx#receiver.com");
//subject of the email
mM.Subject = "your subject line will go here";
mM.Body = "Body of the email";
mM.IsBodyHtml = true;
//SMTP client
SmtpClient sC = new SmtpClient();
//credentials to login in to hotmail account
sC.Credentials = new NetworkCredential(username, password);
//port number for Hot mail
sC.Port = 25;
sC.Host = "smtpout.asia.secureserver.net";
sC.UseDefaultCredentials = false;
//enabled SSL
sC.EnableSsl = false;
sC.DeliveryMethod = SmtpDeliveryMethod.Network;
sC.Timeout = 40000;
//Send an email
sC.Send(mM);
}
catch (Exception ex)
{
var temp = ex.Message;
}
I have also used port no 465 with enablessl = true but no success
I was struggling with this too and found a solution.
The problem is that GoDaddy uses Implicit SSL (SMTPS) and this is NOT supported with System.Net.Mail.
It should be possible to use a GoDady Relay account, but then you can only send 250 emails per day AND the email sent will be SPAM unvisible at receiver side!
Then I found an open source library: http://sourceforge.net/projects/netimplicitssl/
You can get this package via NuGet in Visual Studio.
Search for: Aegis Implicit Mail.
I can tell you that this works perfectly !
private void _SendEmail()
{
try
{
var mail = "YourEmail#YourGoDaddyWebsite.com";
var host = "smtpout.europe.secureserver.net";
var user = "YourEmail#YourGoDaddyWebsite.com";
var pass = "YourGoDaddyPassword!";
//Generate Message
var message = new MimeMailMessage();
message.From = new MimeMailAddress(mail);
message.To.Add("receiver#website.com");
message.Subject = "Subject Text...";
message.Body = "Body Text...";
//Create Smtp Client
var mailer = new MimeMailer(host, 465);
mailer.User = user;
mailer.Password = pass;
mailer.SslType = SslMode.Ssl;
mailer.AuthenticationMode = AuthenticationType.Base64;
//Set a delegate function for call back
mailer.SendCompleted += compEvent;
mailer.SendMailAsync(message);
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
private void compEvent(object sender, AsyncCompletedEventArgs e)
{
if (e.UserState != null)
Console.Out.WriteLine(e.UserState.ToString());
Console.Out.WriteLine("is it canceled? " + e.Cancelled);
if (e.Error != null)
Console.Out.WriteLine("Error : " + e.Error.Message);
}
you should note you cannot sent non-html, or plain text emails. The message.IsBodyHtml member does not seem to work currently.
I am trying to hit gmail smtp server from my C# web application, But getting following errors :
1) Failure Sending Message
2) "The operation has timed out."
Following is my code,
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(to);
message.Subject = Name;
message.IsBodyHtml = true;
message.Body = "Some Message";
smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
smtpClient.Port = 465; //587 for TLS 465 or 25 SSL
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(from, fromPassword);
smtpClient.Timeout = 50000;
smtpClient.Send(message);
}
catch (Exception ex)
{
if (ex.InnerException != null)
{
Console.WriteLine("InnerException is: {0}", ex.InnerException);
}
}
Please Suggest i have tried using Port 587 as well, but both showing the same error.
This link below in greatest details explains how you can do this (in several ways)
static void Main(string[] args)
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Your gmail email address
oMail.From = "gmailid#gmail.com";
// Set recipient email address
oMail.To = "support#emailarchitect.net";
// Set email subject
oMail.Subject = "test email from gmail account";
// Set email body
oMail.TextBody = "this is a test email sent from c# project with gmail.";
// Gmail SMTP server address
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// Set 465 port
oServer.Port = 465;
// detect SSL/TLS automatically
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
// Gmail user authentication
// For example: your email is "gmailid#gmail.com", then the user should be the same
oServer.User = "gmailid#gmail.com";
oServer.Password = "yourpassword";
try
{
Console.WriteLine("start to send email over SSL ...");
oSmtp.SendMail(oServer, oMail);
Console.WriteLine("email was sent successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
}
I´m trying to configure my console application to send an email to the gmail relay server.
the code i´m using is the next :
class Program
{
static void Main(string[] args) {
string header;
string body;
string emailTo;
string emailFrom = "anton.selin#inbox.com";
Console.WriteLine("Enter the header : ");
header = Console.ReadLine();
Console.WriteLine("Enter the message body : ");
body = Console.ReadLine();
Console.WriteLine("Enter your email : ");
emailTo = Console.ReadLine();
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 465);
smtpClient.Credentials = new System.Net.NetworkCredential("anton.selin1#gmail.com", "**********");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress(emailFrom,"anton selin");
mail.To.Add(new MailAddress(emailTo));
mail.Body = body;
mail.Subject = header;
Console.WriteLine("Sending email...");
smtpClient.Send(mail);
Console.WriteLine("Email sent...");
Console.ReadKey();
}
}
when i run the code it gives me a timeout error :
{"The operation has timed out."}
how can i configure my application to be able to send email from console app or web app(from localhost)?
Check for this, I think this is your issue
string emailFrom = "anton.selin#inbox.com";
smtpClient.Credentials = new System.Net.NetworkCredential("anton.selin1#gmail.com", "**********");
mail.From = new MailAddress(emailFrom,"anton selin");
you are using gmail to send your mail whereas you should be using anton.selin#inbox.com credentials to send mail and not Gmail's
Try this code to mail :
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(Username);
mail.To.Add(emailTo);
mail.Subject = Subject;
mail.Body = Body;
SmtpServer.Port = 587; // Also Add the port number to send it, its default for Gmail
SmtpServer.Credentials = new System.Net.NetworkCredential(Username, Password);
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 20000; // Add Timeout property
SmtpServer.Send(mail);
let me know if it works.
Here I am in a strange situation. When I send the email from localhost it is working fine but when I upload the page to the server and try to send email, I get the following error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
I'm displaying the error message in a label for testing purpose and the try is also misplaced I know, I will set it later.
the code I am using is
if (Page.IsValid)
{
try
{
StringBuilder message = new StringBuilder();
message.Append("Hello My Name is ");
message.Append(txtName.Text);
message.AppendLine();
message.AppendLine("My Contact Number " + txtContactNumber.Text.ToString());
message.AppendLine();
message.AppendLine();
message.AppendLine("My Email Id Is " + txtFromEmailAddress.Text.ToString());
message.AppendLine();
message.Append(txtEmailBody.Text);
MailMessage mailMessage = new MailMessage("xxx#gmail.com", "yyy#gmail.com");
mailMessage.Subject = "New Client Query";
mailMessage.Body = message.ToString();
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 25);
//smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "xxx#gmail.com",
Password = "password"
};
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
txtContactNumber.Text = "";
txtFromEmailAddress.Text = "";
txtName.Text = "";
txtEmailBody.Text = "";
lblEmailStatus.Text = "Email Sent Successfully.";
lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
}
catch(Exception ex)
{
lblEmailStatus.Text = ex.Message + " <br> " + ex.Source;
}
}
else
{
lblEmailStatus.Text = "Error! Email Not Sent ";
lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
}
I have googled for a couple of hours and checked links at this site as well but I still cant figure it out.
Now I request you guys here if any one have any solutions / hint.
Try this
public string SendEmailTest(String EmailMessage, String FromMail, String MailPassword, String MailServer, String To, String CC, String BCC, String DisplayName, String Subject, String Attachment)
{
try
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress;
fromAddress = new MailAddress(FromMail);
smtpClient.Host = MailServer;
smtpClient.Port = 25;
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMail, MailPassword);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = SMTPUserInfo;
message.From = fromAddress;
message.To.Add(new MailAddress(To, DisplayName));
if (CC != "")
message.CC.Add(new MailAddress(CC, DisplayName));
if (BCC != "")
message.Bcc.Add(new MailAddress(BCC, DisplayName));
message.Subject = Subject;
message.IsBodyHtml = true;
message.Body = EmailMessage;
if (Attachment != "")
message.Attachments.Add(new Attachment(Attachment));
message.Priority = MailPriority.High;
smtpClient.Send(message);
return "SendEmail";
}
catch (Exception ex)
{
return "Email :" + ex;
}
}
i got the reason finally .
the email i was sending emails from was kind of hacked some days before and for the security reasons google team had kind of marked my email as unsecure . i changed the emails address and it is working fine thanks you all.
Based on the Google Gmail Documentation it would appear that the Port should be 587 not 25. I found a few other questions that seem to be related here and here.
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
I am sending mail but it is giving me exception
Mailbox name not allowed. The server response was: sorry, relaying denied from your location [182.72.17.210] (#5.7.1)
my code
public static string SendMailHtmlFromat(string sForm, string sTo, string sSub, string sMsg)
{
MailMessage mailmsg = new MailMessage();
mailmsg.To.Add(sTo);
mailmsg.From = new MailAddress(sForm);
mailmsg.Subject = sSub;
mailmsg.IsBodyHtml = true;
mailmsg.Body = sMsg;
try
{
//SmtpClient smtp = new SmtpClient("smtpout.secureserver.net", 25);
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25);
smtp.Credentials = new System.Net.NetworkCredential("admin#beautiful.com", "Smart123");
smtp.Send(mailmsg);
}
catch (Exception e)
{ }
return "";
}
is their any problem in SmtpClient declaring
Check the below link.
http://www.eggheadcafe.com/community/aspnet/2/10227041/exception-while-sending-mail.aspx