How to send a CC in C#. Create a duplicate - c#

I developing an app which is an email generator, the thing is that I need to send a carbon copy of my email to the same email I am using to send the original one, basically I need to duplicate every email. But when I set the cc email the code simply ignores the CC.
C# Code:
const string fromPassword = "password";
string subject = "Subject " + sXmlArray[0].ToString();
SmtpClient smtp = new SmtpClient();
//Google SMTP Host
smtp.Host = "smtp.gmail.com";
//smtp.Host = "smtp.office365.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("myEmail#domain.com", fromPassword);
smtp.Timeout = 100000;
MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = StrContent.ToString();
//Carbon Copy
//HERE IS THE ISSUE
MailAddress CarbonCopy = new MailAddress("myEmail#domain.com");
message.CC.Add(CarbonCopy);
smtp.Send(message);
Why the code ignores this piece of code? If I declare a different email it works fine?
Can somebody help me out with this issue? Thanks in advance.

Related

Why do I get an error by trying to send a mail with c#?

I'm trying to send an e-mail using C#, but I get this recurring error :
.
Can you explain me what's wrong with my code ?
Here it is :
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.Timeout = 100000;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("myEmailAdress#gmail.com", "myPassword");
MailMessage msg = new MailMessage();
msg.To.Add("receiver#gmail.com");
msg.From = new MailAddress("sender#gmail.com");
msg.Subject = "My subject";
msg.Body = "My body";
client.Send(msg);
MessageBox.Show("Message sent !");
I have encountered same before.
You are getting this error because you haven't set ON on Less secure app access for sender#gmail.com as you are using Gmail SMTP port.
Reason:
Your email has no remotely access permission. You have to configure
it. Suppose you want to sent email from sender#gmail.com so you
have set that permission NO to this account.
How To Set:
You could try like below
Or can open that tab from this link directly Less secure app access
Update:
As per your comment this is for you which has working perfectly since the beginning of my career
public 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
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;
// mail.Attachments.Add(new Attachment(#attacmmentLocationPath));
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(senderName, senderPass);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
return true;
}
catch (Exception ex)
{
return ex;
}
}
Hope that would help.
This code works for gmail, it is very similar to yours with slight differences, but if you try this, and it doesn't work for you, the issue is not the code - perhaps some other network related issue that you are going to need to fix first:
using (var msg = new MailMessage())
{
msg.From = new MailAddress("fromaddress#gmail.com");
msg.To.Add("toaddress#gmail.com");
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("xxx#gmail.com","password");
smtp.Send(msg);
}
}
An SMTP client may log in using an authentication mechanism chosen among those supported by the SMTP servers.

How to send email through SMTP to email address contain non-ASCII word

How to send email through SMTP to email address which contain non-ASCII word.
Consider an users with email id : téést#test.com
Code is not throwing any exception and while checking the delivery report it gave an error message:
"Technical details of permanent failure:
local-part of envelope RCPT address contains utf8 but remote server did not offer SMTPUTF8 ".
I am using .net 4.5 and my code sample is :
MailMessage message = new MailMessage();
message.From="test#test.com";
message.Subject = "subject";
message.IsBodyHtml = true;
message.Body = "body";
message.To.Add("téést#test.com");
SmtpClient smtp = new SmtpClient();
smtp.Host = "SMTPServer";
smtp.Port = "SMTPPort";
var userName = "SMTPUserName";
var password = "SMTPPassword";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(userName, password);
smtp.EnableSsl = true;
smtp.DeliveryFormat = SmtpDeliveryFormat.International;
smtp.Send(message);
Thanks In Advance.

.Net email integration won't send using godaddy SMTP Credential

Hi its been almost a day that I've been figuring things out with regards to sending email from godaddy email account to a gmail account. I have had my research online and almost tried everything but no luck .. here's what I made so far.
protected void generateEmail(){
MailMessage mail = new MailMessage ();
mail.From = new System.Net.Mail.MailAddress ("contact#company.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient ();
smtp.Port = 465; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential ("contact#company.com", "password123!"); // [4] Added this. Note, first parameter is Email Address of the sender and the next parameter is the password.
smtp.Host = "relay-hosting.secureserver.net";
//recipient address
mail.To.Add (new MailAddress ("test#gmail.com"));
mail.To.Add (new MailAddress ("testagain#gmail.com"));
//Formatted mail body
mail.IsBodyHtml = true;
string st = "This is a Test Message";
mail.Body = st;
smtp.Send (mail);
}
Can anyone help me out ? would appreciate any hands..
protected void generateEmail()
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("whereEmailWillBeSent#gmail.com");
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("mail#company.com");
msg.From = address;
msg.Subject ="Hi this is mail from company";
msg.Body = "Your Message";
SmtpClient client = new SmtpClient();
//for Godaddy
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
client.EnableSsl = false;
client.UseDefaultCredentials = false;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
}
}

Unable to connect to remote server while sending email in asp.net

The above error arising while using email sending code my code is
string fromAddress = "mymail";
string fromPassword = "mypassword";
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;
smtp.Send(fromAddress, toAddress, MailSubject, Body);
I googled many times but didnt get proper solution.
Port 587 is enbled and firewall blocking also not there.
try
{
MailMessage mail = new MailMessage();
mail.To.Add("sender id");
mail.From = new MailAddress("your id");
mail.Subject = "Mail from my web page";
mail.Body ="Body Content";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
//Or your Smtp Email ID and Password
smtp.Credentials = new System.Net.NetworkCredential
("XYZ", "XXXXX");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
//display exception
}
This code work for me.Try this.
MailMessage mM = new MailMessage();
mM.From = new MailAddress("YourGmail#gmail.com");
mM.To.Add(Email);
mM.Subject = "Your Sub";
mM.Body = "Your Body" ;
mM.IsBodyHtml = true;
mM.Priority = MailPriority.High;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential("YourGmail", "YourPassword");
//sC.EnableSsl = true;
sC.EnableSsl = true;
sC.Send(mM);
Thanks for the response.I got solution as
if you are using any antivirus software check it's log to see whether it is because of the antivirus. I faced same problem when McAffee was blocking my mails (there is a security policy - Prevent mass mailing worms from sending mails). Edit this policy and add your application to the exception list. In my case this sorted the problem. Please check if it works for you.
I use following code for Gmail:
Function SendMail_Gmail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String) As Boolean
Dim mailmsg As New System.Net.Mail.MailMessage()
mailmsg.From = New MailAddress(strFrom)
mailmsg.To.Add(strTo)
mailmsg.Subject = strSubject
mailmsg.IsBodyHtml = True
mailmsg.Body = strBody
mailmsg.Priority = System.Net.Mail.MailPriority.Normal
Dim client As New System.Net.Mail.SmtpClient()
client.Host = "smtp.gmail.com"
client.Port = "587"
client.Credentials = New System.Net.NetworkCredential("youremailid#gmail.com", "Yourpassword")
client.EnableSsl = True
Dim userstate As Object = mailmsg
client.Send(mailmsg)
Return True
End Function

Which Content-Transfer-Encoding when sending emails via C# or there are not any difference?

I am sending emails via C# the way below
MailMessage mail = new MailMessage();
mail.To.Add(txtTestEmail.Text);
mail.From = new MailAddress("noreply#monstermmorpg.com", "MonsterMMORPG");
mail.Subject = "Test email from MonsterMMORPG";
mail.Body = "This is test email sent from MonsterMMORPG";
mail.IsBodyHtml = true;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.BodyEncoding = System.Text.Encoding.UTF8;
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.Host = "127.0.0.1";
smtp.Port = 25;
smtp.Send(mail);
It is default base64 i suppose. But i wonder which Content-Transfer-Encoding is best for sending emails via using windows server 2008 r2 default smtp server.
Are there any difference between different Content-Transfer-Encodings . Thank you.

Categories