Send email with System.Web.Mail - asp.net

I want send email in asp.
I use this code
using System.Web.Mail;
MailMessage msg = new MailMessage();
msg.To = "aspnet#yahoo.com";
msg.From = "info#mysite.com";
msg.Subject = "Send mail sample";
msg.BodyFormat = MailFormat.Html;
string msgBody="Hello My Friend. This is a test.";
msg.Body = msgBody ;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(msg);
But i get error :
Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
How to send email with asp?

I use This code .
MailMessage msg = new MailMessage();
msg.Body = "Body";
string smtpServer = "mail.DomainName";
string userName = "info#mysite.com";
string password = "MyPassword";
int cdoBasic = 1;
int cdoSendUsingPort = 2;
if (userName.Length > 0)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
msg.To = user.Email;
msg.From = "info#Mysite.com";
msg.Subject = "Subject";
msg.BodyEncoding = System.Text.Encoding.UTF8;
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(msg);

You might need to provide credentials.
example:
smtpMail.Credentials = new NetworkCredential("username", "password")

If you are trying to send email without authenticating, I am afraid that's impossible. If any users in your web site can send emails without password, that's horrible. It will allow user to send email from other person account. So considering security, sending email will required to provide email address and password
var fromAddress = ""; // Email Address here. This will be the sender.
string fromPassword = ""; // Password for above mentioned email address.
var toAddress = "";// Receiver email address here
string subject = "Hi";
string body = "Body Text here";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com"; // this is for gmail.
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(fromAddress, toAddress, subject, body);
[Edited]
Sorry My mistake i didn't noticed that. They both used for same purpose. If you are using higher version (2.0 or later) of .Net framework, then use System.Net.Mail. If you use System.Web.Mail it only shows a warning saying this is deprecated. But that will work.
Here is the answer for System.web.mail
MailMessage mail = new MailMessage();
mail.To.Add("to#domain.com");
mail.From = new MailAddress("from#domain.com");
mail.Subject = "Email using Gmail";
mail.Body = "";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(mail.From,"YourPassword");
smtp.Send(mail);

Related

Send email using gmail: The operation has timed out

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.

Unable to send Emails from Remote Server using asp.net & C#?

I am able to send emails successfully from LocalHost.
After hosting my website i am unable to send.
The following is the error:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. ha10sm40374374pbc.23 - gsmtp
can anyone help me to solve this.
Below is code:
public void SendMail(string ToMail, string subject, string Message)
{
// Gmail Address from where you send the mail
string fromAddress = "mygmailid#gmail.com";
// any address where the email will be sending
//string toAddress = ToMail;
//Password of your gmail address
const string fromPassword = "********";
// Passing the values and make a email formate to display
//string subject = subject;
string body = "\n\n"+Message;
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, ToMail, subject, body);
}
Google sent a mail with the subjact "Suspicious sign in prevented"
I selected it's me only option.
Then changed the Gmail password and used it in the application .
Now able to send the mails from the production server also...
Try below code:
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(email);
mailMessage.Subject = "YOUR_SUBJECT";
mailMessage.Body = "YOUR_MAIL_BODY";
mailMessage.To.Add(new MailAddress("RECIPIENT_EMAILID"));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new
System.Net.NetworkCredential();
NetworkCred.UserName = "SENDER_MAIL";
NetworkCred.Password = "SENDER_MAIL_PASSWORD";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Send(mailMessage);
}
After invoking above method GOOGLE or GMAIL will send you one mail regarding security (To allow the less secure app to configure mail) then allow that one.

Asp.net 4.0 Sending Emails

I have aspx page where i have the email field like this
<input class="span12" type="text" placeholder="EMAIL" id="Email" name="Email" runat="server" />
In my Csharp file i have code and using Request["Email"] to get the address when visitor enter the email address which can be any so i want to email them as well my code is like Below, But it does not work, I am using .net 4.0, where i can change then that dynamic email whatever it would be i could get it and send email.
private void SendEmail(int RefNum)
{
var customerEmail = Request["Email"]; //getting value from aspx page.
MailMessage ObjEmail = new MailMessage();
ObjEmail.SendFrom = "carlaza#hotmail.ca";
ObjEmail.SendTo = "zjaffary#hotmail.com";
ObjEmail.SendCC = "jaffary_zafar#hotmail.com";
ObjEmail.SendBCC = customerEmail ;
ObjEmail.Subject = "test Subject ";
//Development
//SmtpMail.SmtpServer = "tormail.corp.kkt.ca";
//Production At Bell
SmtpMail.SmtpServer = "tormail.corp.kkt.ca";
ObjEmail.BodyFormat = MailFormat.Html;
string strBody1 = "Test message " ;
ObjEmail.Priority = MailPriority.High;
try {
SmtpMail.Send(ObjEmail);
lblResponse.Text = "Thank you for sending the form !";
Response.AddHeader("Refresh", "2;URL=index.aspx");
}
catch (Exception exc){
Response.Write("Send failure: " + exc.ToString());
}
}
You should use authetication information from web mail server. (user name and password) If not, that is not actual e-mail.
You can see the code and can work
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("youremail#hotmail.com");
mail.To.Add("to#gmail.com");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("youremail#hotmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Please see the topic. I think it is helpfull for you.
How to add smtp hotmail account to send mail
How to add smtp hotmail account to send mail
try this
protected void sendEmail(string subject, string ToEmail, string msg)
{
String body = msg;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("your_email_id");
smtpClient.Host = "smtp.gmail.com";//host
smtpClient.Port = 587;//port no. default 25
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("your_email_id", "password");
message.From = fromAddress;
message.To.Add(ToEmail);//if more than comma seprated
message.Subject = subject;
message.Priority = MailPriority.High;
message.Body = body;
message.IsBodyHtml = true;
smtpClient.Send(message);
}

how to attach a file in email using shared hosting server?

I want to attach a file dynamically and send it through mail. So can someone please let me know how can i send it with attachment.
You can use this code.
using System.Net.Mail;
private void SendEmail()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("YourEmailAddress#domain.com");
message.To.Add(new MailAddress("Recipient#domain.com"));
message.Subject = "Subject";
message.Body = "Email Message Body";
// Add attachment
string attachmentPath = Server.MapPath("~/AttachmentPath.jpg");
message.Attachments.Add(new Attachment(attachmentPath));
// Connect to GoDaddy SMTP
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net");
smtp.Credentials = new System.Net.NetworkCredential("Username","Password");
smtp.Port = 25;
smtp.EnableSsl = false;
// Send the message
smtp.Send(message);
}
you can try with this code
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("...");/:Adjust your adress
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);
SmtpServer.Port = ..; //Replace with your port number
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

Sending e-mail in ASP .Net

How to send e-mail on ASP .Net using outlook address??
I've tried this code but no luck:
Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))
mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)
smtpClient.Send(mailMessage) //--> got error here
But while I'm execute the code, it got this error message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
I've tried to add a line of code:
smtpClient.Credentials = New System.Net.NetworkCredential(username, password)
But still can't send the e-mail.
Anyone can help?
Try smtpClient.UseDefaultCredentials = false; before you set new Credentials
Try to set smtpClient.EnableSsl to true / false depending on your environment
I'm guessing you are using Exchange 2007 or later as backend?
Anyway, your mail server does not allow you to send mails anonymously. You'll either need to supply a username/password in your code or allow unauthenticated relaying from your webserver.
Talk to your IT guys what they prefer.
I did it using c#.This may help you.Please check.
MailMessage msg1 = new MailMessage();
msg1.From = strEFrom;
msg1.To = strETo;
msg1.Cc = strECC;
msg1.Subject = "Hi";
msg1.Priority = MailPriority.High;
msg1.BodyFormat = MailFormat.Html;
msg1.Body = strBody;
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
SmtpMail.Send(msg1);
and in web.config file
<appSettings>
<add key="MailServer" value=""/>
</appSettings>
Try these settings check for .EnableSSL property to true/false and the smtp post number on which your mail server listen for outgoing mail.
When you do not set enable the SSL settings in outlook for gmail outlook configuration then it gives same error but the reason was found the SSL settings..
well try this may it will solve your problem..
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
if it does not solve your problem then check your mail server/ client for the problem.
public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
I use this code to send mail from my gmail account.
was having the same error, moved the client.UseDefaultCredentials = false; before setting client.Credentials and everything works.

Resources