Gmail message is rejected - asp.net

I am trying to send emails from ASP.net project using "System.Net.Mail.MailMessage". I need to add about 30 contacts to "To" or "BCC" list.
It works for few recipients but when the number increases;
Gmail give error:
"Message rejected. See http://support.google.com/mail/bin/answer.py?answer=69585 for more information."
And block my accont for sending any more emails for 1 day.
How can i allow gmail to send email to many recipients?
If not what is the alternative way?

You should be able to comfortably send to 30 addresses.
I tried 40 email addresses for CC list (worked for To list as well) with the following test code,
string mailFrom = "test123#gmail.com";
string mailTo = "testccc#domain.com"; //One of your test emails
MailAddress to = new MailAddress(mailTo);
MailAddress from = new MailAddress(mailFrom);
MailMessage mail = new MailMessage(from, to);
mail.Subject = "Test subject";
mail.Body = "Hello world!!!!";
//Keep adding your emails as follows, do it in a for loop
mail.CC.Add("dhanuka1#domain.com");
mail.CC.Add("dhanuka2#domain.com");
...
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mydev#gmail.com", "password");//Your credentials
smtp.EnableSsl = true;
Console.WriteLine("Sending email...");
smtp.Send(mail);
Console.WriteLine("Email sent...");
Console.ReadLine();

Related

ASP.NET how to write code for mail sender?

I have search many sites. But I can't understatnd what they said. Refer in internet they told to create CDO files.
Please help me to send mail in my Project. I have 3 textboxes:
Tomail
Heading
Content of the message
Send Button
Use Below Code to sent mail.
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("me#mydomain.com");
mail.To.Add("u#urdomain.com,rkrishtring#gmail.com");
mail.Subject = "Report";
mail.Body = "This is a Stack overflow using report";
Attachment attachment = new Attachment(filename);
mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

Error loading in ASP.NET Email verification URL at localhost

I am using smtp gmail to for email verification. It is assigned as per the standards. When a new user registers, it is able to send mail to user along with URL for verification. But on clicking the URL which the user receives, it is unable to load the page. What might be the error
the URl address is like
string url = "http://localhost:49432/EmailVerification.aspx?vcode=" + id
this is what my smtp class mail looks like
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Port = 587; // 465; //Gmail works on this port
// smtp.Port = 25;
smtp.Timeout = 100000;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.Send(objmail);
//ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MSG", "alert('Mail Sent Sucessfully');", true);
in the webconfig file. I have given

ASP.NET sending with Gmail smtp overrides From Address

I was trying to send from a aspx page a mail message using the smtp gmail (we are google apps customers and I tried with a paid account and with a free gmail...)
I was able to send the messagge setting credentials, port, ssl, etc. but what is wrong is the From address of the message. Indeed, the mail is coming always from the address of the auth user and not from what I set in the msg.From property...
Eg. the mail arrives with from "myaccount#gmail.com" and not from sender#somedomain.com
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sender#somedomain.com", "sender name");
msg.To.Add(new MailAddress("recipient#somedomain.com","recipient name"));
msg.Subject = "subject";
msg.Body = "some body";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myaccount#gmail.com", "*******");
smtp.EnableSsl = true;
smtp.Send(msg);
Is there a setting to avoid this ?
thanks in advance
Sandro
Check the message headers.
Depending on your email client, here's the instructions how to reach them: https://support.google.com/mail/answer/22454?hl=en
You'll see, that FROM field is only for indication of the sender's name.
It can be ignored by email client, especially, if it contains address different than real sender's address.

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.

Send email with System.Web.Mail

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);

Categories

Resources