I am using the SmtpClient in C# and I will be sending to potentially 100s of email addresses. I don't want to have to loop through each one and send them an individual email.
I know it is possible to only send the message once but I don't want the email from address to display the 100s of other email addresses like this:
Bob Hope; Brain Cant; Roger Rabbit;Etc Etc
Is it possible to send the message once and ensure that only the recipient's email address is displayed in the from part of the email?
Ever heard of BCC (Blind Carbon Copy) ? :)
If you can make sure that your SMTP Client can add the addresses as BCC, then your problem will be solved :)
There seems to be a Blind Carbon Copy item in the MailMessage class
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.bcc.aspx
Here is a sample i got from MSDN
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an e-mail message from an application very easily.";
MailAddress bcc = new MailAddress("manager1#contoso.com");
//This is what you need
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an e-mail message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString() );
}
}
If you are using the MailMessage class, make use of the BCC (Blind Carbon Copy) property.
MailMessage message = new MailMessage();
MailAddress bcc = new MailAddress("manager1#contoso.com");
// Add your email address to BCC
message.Bcc.Add(bcc);
Related
I have a list of data from a database and I have Email Id's in database, i have to send email to those ID's where if its retention date is tomorrow means i have to send an intimation email as reminder by today,i want to send email, and i will use service to send it daily, but my email part is not working..below is my code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace SendingEmail
{
public class SendingMail
{
public static void SendMail(string recipient, string subject, string
body, string attachmentFilename)
{
//method to send email
MailMessage mail = new MailMessage();
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "my password");
string From = "my email#gmail.com";
string To = "email.com";
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
MailMessage msg = new MailMessage(From, To);
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment
(#"C:\Users\rahul.chakrabarty\Desktop\logg.txt");
mail.Attachments.Add(attachment);
SmtpServer.Send(mail);
}
//To cathch Exception
catch (Exception ex)
{
Console.WriteLine("unable to send" + ex);
}
}
}
}
My error is "unable to sendsystem.InvalidOperationException: A from address must be specified"..This is my error
You said
String from = "my email#gmail...
But nowhere have you actually assigned this string to the mail.From property - you've gone and made a new MailMessage using that from address, and called it msg:
MailMessage msg = new MailMessage(From, To)
but you aren't sending the msg mail, you're sending the mail mail:
SmtpServer.Send(mail);
The same problem exists with the To address on the mail variable
Basically, the code is all messed up: you make TWO MailMessage objects, set half the necessary things on one, the other half of necessary things on the other, and then try to send one of them with incomplete details. It's kinda like you copied and pasted two different tutorials together but didn't get enough of either of them to get a complete MailMessage ready for sending
I could have fixed this all up for you and posted working code, but I haven't for two reasons: 1) it's very easy to do these small changes yourself, and 2) I want YOU to do it as a learning exercise rather than just giving you the answer :)
Make sure you don't put a space in the email address either
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
smtpClient.Credentials = new NetworkCredential("xxxxx#gmail.com", "password");
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//mail.From = new MailAddress("xxx1#gmail.com");
mail.To.Add("XX2#gmail.com");
mail.Subject = "Test mail";
mail.Body = "This is test mail, with test content";
smtpClient.Send(mail);
In the above code, why mail.From is mandatory? even I specified mail-id in smtpClient.Credentials.
Even I specified mail.From with some mail-id, the receiver is not receiving mail from this mail address, instead receiving from xxxxx#gmail.com which I specified in smtpClient.Credentials.
From is supposed to be the address from where the mail was sent.
This can be different from your Smtp credentials. Some servers will let you have a username that is different from your mail address.
Gmail shows the email address from where it received the mail.
They do that so that users don't mistake getting the mail from someone who didn't really send it.
Not all email clients do that.
You cannot fake a from address to gmail. They will always show where they got the email from.
how can i send mass mail through bcc in asp.net(c#)..??
According to the documentation you do it like this:
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an e-mail message from an application very easily.";
MailAddress bcc = new MailAddress("manager1#contoso.com");
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an e-mail message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString() );
}
}
You just add several bcc addresses. Note that sending mass mails will easily get you blacklisted and most of your messages will probably end up in spam folders. You should consider using a mass mail service provider for this instead.
You can make you of the System.Net.Mail namespace classes for send out mails in .net applications. Refer to MSDN documentation: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
How to send the email through mine company email id
Like every company use to give some email id to its associates like xyz#abc.com
How to send the mails through this id to other accounts. Also I don't have passwords for this id, then how to send the email in this situation.
You could use the SmtpClient class. Other than the from and to addresses you will need your company's SMTP server:
var message = new MailMessage();
message.To.Add("to#abc.com");
message.Subject = "This is the Subject";
message.From = new MailAddress("from#abc.com");
message.Body = "body of the mail";
using (var smtpClient = new SmtpClient("smtp.abc.com"))
{
smtpClient.Send(message);
}
I am trying to create a contact us page where visitor of the site can leave a message to site-admin.
say I m running this site
www.example.com
and i have an id contact#example.com
Now a new visitor visits my site and fills up the contact us form.
He fills
FROM - abc#yahoo.com
Subject- Query
Message- My question....
I want to send this message as a mail to contact#example.com
And on successfull sending of mail i want to send this user autogenerated mail as an acknowledgement.
What I need to do for this, My hosting server is GoDaddy
I am trying the following code.
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(txtEmailId.Text);
mailMessage.To.Add(new MailAddress("contact#example.com"));
mailMessage.Subject = txtSubject.Text;
mailMessage.Body = txtMessage.Text;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.High;
SmtpClient sc = new SmtpClient("relay-hosting.secureserver.net");
//sc.Credentials = new System.Net.NetworkCredential("contact#example.com", "MyPassword");
sc.Send(mailMessage);
}
catch (Exception ex)
{
Label1.Text = "An Error has occured : "+ex.Message;
}
This error occurs while trying to send mail.
Mailbox name not allowed. The server
response was: sorry, your mail was
administratively denied. (#5.7.1)
It looks like from your code you are specifying that the mail message's From field is based on the email address the user fills in. The mail server mail be rejecting this and only allowing email from the domain name example.com or perhaps the server name itself. I've run into this before where your From field must be the original site domain...
If you want to have the ability for the form-mail recipient to directly reply, use the ReplyTo field rather than the from:
MailMessage mailMessage = new MailMessage();
//try using a domain address that matches your server and/or site.
//the email address itself may also have to exist depending on the mail server.
mailMessage.From = new MailAddress("no-reply#example.com");
//set the replyto field
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
//send the mail...
Hope this might help...
EDIT:
After you've edited your question, the above is still relevant. It's very likely you cannot use a From address outside of your own site domain example.com. If you want to send two copies of the request (one to contact#example.com and the other to the user as an acknowledgment), you simply need to add to the To property:
MailMessage mailMessage = new MailMessage();
//try using a domain address that matches your server and/or site.
//the email address itself may also have to exist depending on the mail server.
mailMessage.From = new MailAddress("abc#example.com");
//add the contact email address to the To list
mailMessage.To.Add(new MailAddress("contact#example.com"));
//add the user email address to the To list
mailMessage.To.Add(new MailAddress(txtEmailId.Text));
//set the replyto field
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
//send the mail...
If you need to add a simple message for the user acknowledgment email you could also do:
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("abc#example.com");
mailMessage.To.Add(new MailAddress("contact#example.com"));
mailMessage.ReplyTo = new MailAddress(txtEmailId.Text);
mailMessage.Subject = txtSubject.Text;
mailMessage.Body = txtMessage.Text;
mailMessage.IsBodyHtml = false;
mailMessage.Priority = MailPriority.High;
SmtpClient sc = new SmtpClient("relay-hosting.secureserver.net");
//sc.Credentials = new System.Net.NetworkCredential("contact#example.com", "MyPassword");
//send to only the contact#example.com first
sc.Send(mailMessage);
mailMessage.To.Clear();
mailMessage.To.Add(new MailAddress(txtEmailId.Text));
//add a simple message to the user at the beginning of the body
mailMessage.Body = "Thank you for submitting your question. The following has been submitted to contact#example.com: <br/><br/>" + mailMessage.Body;
//send the acknowledgment message to the user
sc.Send(mailMessage);
}
catch (Exception ex)
{
Label1.Text = "An Error has occured : "+ex.Message;
}
It is probably Serverfault.com question, any how
you need to check these options
Admin Home -> Configuration -> Email
Options -> - E-Mail Transport Method =
sendemail
reference 553 sorry, your mail was administratively denied. (#5.7.1)