send mail through bcc - c#

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

Related

Access denied when sending a mail from C# programm

I'm developing a third-party add-on to run in a program called M-Files.
The purpose of the add-on is to send a mail with the help of an SMTP server. I created a fake SMTP server in DevelMail.com just for testing.
Testing the SMTP server from a browser works but when i run the code it gives me the following error.
Transaction failed. The server response was: 5.7.1 Client host rejected: Access denied
Here are the SMTP information:
Host: smtp.develmail.com
SMTP Port: 25
TLS/SSL Port: 465
STARTTLS Port : 587
Auth types: LOGIN, CRAM-MD5
Here is the code:
MailAddress adressFrom = new MailAddress("notification#mfiles.no", "M-Files Notification Add-on");
MailAddress adressTo = new MailAddress("majdnakhleh#live.no");
MailMessage message = new MailMessage(adressFrom, adressTo);
message.Subject = "M-Files Add-on running";
string htmlString = #"<html>
<body>
<p> Dear customer</p>
<p> This is a notification sent to you by using a mailadress written in a metadata property!.</p>
<p> Sincerely,<br>- M-Files</br></p>
</body>
</html>
";
message.Body = htmlString;
SmtpClient client = new SmtpClient();
client.Host = "smtp.develmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("myUserName", "myPassword");
client.EnableSsl = true;
client.Send(message);
Reason for the Issue:
Usually, email sending option using SMTP encountered Access denied
because there should have a sender email which required to allow
remote access. When SMTP request sent from the sender email
it checks whether there is remote access allowed. If no, then you
always got Access denied message.
Solution:
For example let's say, you want to send email using Gmail SMTP in that case you do have to enable Allow less secure apps: ON
How To Set
You can simply browse this link Less secure app access and turn that to ON
See the screen shot
Code Snippet:
public static object SendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, string senderName, string senderPass, string attacmmentLocationPath)
{
try
{
MailMessage mail = new MailMessage();
//Must be change before using other than gmail smtp
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = mailSubject;
mail.Body = mailBody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);//Enter the credentails from you have configured earlier
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Note: Make sure, fromEmail and (senderName, senderPass) should be same email with the credential.
Hope that would help.

how to send email using smtp in c#?

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

Not able to send mail to group email id

I'm using smtp (c#)and trying to send mail to group id(official id) but its not able to send mail.Although through same code I'm able to send mail to individual id. Any idea what could be wrong here.
Following code I'm using
MailMessage mail = new MailMessage();
mail.To = "group#company.com";
mail.From = "me#company.com";
mail.Subject = "Test Mail: please Ignore";
mail.Body = body;
SmtpMail.SmtpServer = "mailhub.int.company.com";
SmtpMail.Send(mail)
I'm getting following error in my mail box:
Delivery has failed to these recipients or distribution lists:
group#company.com
Not Authorized. You are not authorized to send to this recipient or distribution list. For distribution lists please check approved senders in the corporate directory.
_____
Sent by Microsoft Exchange Server 2007
Diagnostic information for administrators:
Generating server: GSPWMS005.int.company.com
group#company.com
#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##
From error I could get an idea that some authentication is missing but not sure which one or how to resolve it.
If I send mail to this group thorough my outlook then its working ok.
http://msdn.microsoft.com/en-us/library/59x2s2s6.aspx
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
message.Subject = "Test Email using Credentials";
NetworkCredential myCreds = new NetworkCredential("username", "password", "domain");
CredentialCache myCredentialCache = new CredentialCache();
try
{
myCredentialCache.Add("ContoscoMail", 35, "Basic", myCreds);
myCredentialCache.Add("ContoscoMail", 45, "NTLM", myCreds);
client.Credentials = myCredentialCache.GetCredential("ContosoMail", 45, "NTLM");
client.Send(message);
Console.WriteLine("Goodbye.");
}
catch(Exception e)
{
Console.WriteLine("Exception is raised. ");
Console.WriteLine("Message: {0} ",e.Message);
}

Sending to multiple Email addresses but displaying only one C#

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

Receiving Emails on Hosting Domain Email Id from any user visiting to site

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)

Categories