How can I sendEmail to two addresses at once? - asp.net

I'm new to ASP.NET, so I hope I'm providing the right context and information here.
I have a (pre-existing) form that looks like this:
protected void sendEmail(object sender, EventArgs e)
{
try
{
this.recaptcha.Validate();
if (this.recaptcha.IsValid == true)
{
lblResult.Visible = false;
String from = "donotreply#website.com";
String to = "test2#website.com";
String to2 = "test1#website.com";
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(new MailAddress(to));
mail.To.Add(new MailAddress(to2));
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("localhost");
smtp.Send(mail);
}
Based on what I've googled, using mail.To.Add twice is the proper way to send the resulting mail to two separate addresses -- but unfortunately, I've had no luck at all. Nor does separating two addresses using a comma or semicolon work.
What is the proper way to do this?

Related

Sending Mail while running the application in IIS

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(txtEmail.Text.ToString());
mailMessage.Subject = txtSubject.Text.ToString();
mailMessage.To.Add(new MailAddress("my#gmail.com"));
mailMessage.Body = txtSuggestion.Text.ToString();
mailMessage.IsBodyHtml = true;
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new NetworkCredential(txtEmail.Text.ToString(), txtPassword.Text.ToString());
sc.EnableSsl = true;
sc.Send(mailMessage);
lblMessage.Text = "Message Sent Successfully";
txtSubject.Text = "";
txtEmail.Text = "";
txtName.Text = "";
txtSuggestion.Text = "";
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
This is the code i am using to send email but whenever i am trying to run the application, i am getting error.
enter image description here
if someone can help why i am getting the error, that would be great.
When using SSL, try using port 465.
https://support.google.com/a/answer/176600
You also have another issue with that code that's un-related to the one you're subscribing. When setting up contact us forms, using the person's email as the FROM field will cause DMARC to fail. You can read up about that here
Also I don't understand why you are using the person's email address and password to connect to gmail with. You should use your own account if you're sending mail.

sending Email with current path of asp file

I want to create a asp file that once its opened it will send an Email to me with its Current location in Server, I am really new to ASP and I searched the internet on how to do so but I failed, also there is no Mail server on the server, any Ideas ?
Just replace example data with yours.
I assume that by "Current location in Server" your mean reqest path.
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var mail = new MailMessage();
mail.Subject = "Subject";
mail.Body = string.Format("<p>{0}</p>", Request.Path);
mail.IsBodyHtml = true;
mail.From = new MailAddress("sender#exampledomain.com");
mail.To.Add("recipient#exampledomain.com");
var smtp = new SmtpClient();
smtp.Host = "mail.exampledomain.com";
smtp.Credentials = new NetworkCredential("sender#exampledomain.com", "{sender_password_here}");
smtp.Send(mail);
}
}

Sending an email with ASP .Net and Gmail SMTP

I am developping an ASP .Net website.
One of my ASPX page contains a button.
When this button is clicked I want to send an email by using the Gmail SMTP server.
I want to use my Gmail account as credentials.
Here is the button's click event handler :
protected void m_myButton_Click(object p_sender, EventArgs p_args)
{
string l_subject = "My subject";
StringBuilder l_builder = new StringBuilder();
// ...
string l_body = l_builder.ToString();
string l_host = ConfigurationManager.AppSettings["SmtpHost"];
int l_port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
NameValueCollection l_smtpAccount = (NameValueCollection)ConfigurationManager.GetSection("smtpAccount");
string l_address = l_smtpAccount["SmtpAddress"];
string l_password = l_smtpAccount["SmtpPassword"];
MailMessage l_mail = new MailMessage();
l_mail.From = new MailAddress(l_address);
l_mail.To.Add(l_address);
l_mail.Subject = l_subject;
l_mail.Body = l_body;
SmtpClient l_smtp = new SmtpClient();
l_smtp.Host = l_host; // l_host = "smtp.gmail.com"
l_smtp.Port = l_port; // l_port = 587
l_smtp.EnableSsl = true;
l_smtp.UseDefaultCredentials = false;
l_smtp.Credentials = new NetworkCredential(l_address, l_password);
l_smtp.Send(l_mail);
}
The command line l_smtp.Send(l_mail) fails.
A SMTP exception is raised and the InnerException property informs me that (translation from French to English) : No connection has been etablished because the target computer has refused it 173.194.67.108:587
My code is based on code found on this page : http://www.codeproject.com/Questions/254743/sending-email-in-asp-net-using-smtp-gmail-server
Any help will be greatly appreciated
Try this...
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("Email ID where email is to be send");
mail.To.Add("Another Email ID where you wanna send same email");
mail.From = new MailAddress("YourGmailID#gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName#gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
No connection has been etablished because the target computer has refused it 73.194.67.108:587
Try the other SMTP port: 465
I have turned my antivirus program off as advised by Rup and I have not got any exception.
I will created a custom rule in my antivirus program to allow communication between my ASP .Net website and the Gmail SMTP server.

Sending mass email in ASP.NET

This is my code to send lots of emails. I want to optimize this code to be sure that it will work and can successfully send all emails. What should I do? I know that putting interrupts between sending might be useful but how can I do this?
The main problem is avoiding classify emails as spam and decreasing number of failed sent emails.
var list = from c in context.Emails orderby c.EmailAddress select c.EmailAddress;
MailMessage mail = new MailMessage();
try
{
mail.From = new MailAddress(txtfrom.Text);
foreach (var c in list)
{
mail.To.Add(new MailAddress(c.ToString()));
}
mail.Subject = txtSub.Text;
mail.IsBodyHtml = true;
mail.Body = txtBody.Text;
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(
FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
catch (Exception)
{
//exception handling
}
I would advise you against adding all reciepients into the same mail message.
Rather use this code:
mail.From = new MailAddress(txtfrom.Text);
mail.Subject = txtSub.Text;
mail.IsBodyHtml = true;
mail.Body = txtBody.Text;
if (FileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
foreach (var c in list)
{
mail.To.Clear();
mail.To.Add(new MailAddress(c.ToString()));
smtp.Send(mail);
}
With a little due diligence, this can be accomplished with a very simple console application which can be called from the Web form to dispatch the emails. By diligence, I mean to insert a pause between batches so that the mail server won't get bogged down. For example, if you were grabbing the addresses from a DB and sending them out, you could have something like:
if ((count >= 100) && (count % 100 == 0))
Thread.Sleep(30000);
-----------------------------------------
// Web form code-behind
// Pass subject and message strings as params to console app
ProcessStartInfo info = new ProcessStartInfo();
string arguments = String.Format(#"""{0}"" ""{1}""",
subjectText.Text.Replace(#"""", #""""""),
messageText.Text.Replace(#"""", #""""""));
info.FileName = MAILER_FILEPATH;
Process process = Process.Start(info.FileName, arguments);
Process.Start(info);
More info here: Calling Console Application from Web Form

Is there any way to send the Email notification using asp.net without using credentials?

I have this code to send email notification in my page.
MailAddress to = new MailAddress("xxxxx#gmail.com");
MailAddress from = new MailAddress("xxx#gmail.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Error Occred in the application:";
message.Body = ex.Message;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("user", "password");
Is there any other way we have without giving credentials to send the message?
There are some workarounds mentioned here
Only on servers that allow anonymous sending, which Gmail doesn't.
If you have an open mail relay that doesn't require credentials, then you don't need to supply them.
use this code spinet for that,
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailAddress SendFrom = new MailAddress(txtFrom.Text);
MailAddress SendTo = new MailAddress(txtTo.Text);
MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
MyMessage.Subject = txtSubject.Text;
MyMessage.Body = txtBody.Text;
Attachment attachFile = new Attachment(txtAttachmentPath.Text);
MyMessage.Attachments.Add(attachFile);
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(MyMessage);
litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}
i hope this will be helpful.

Resources