Sending email problem by Gmail account - c#

I'm trying to send a email in C# using Gmail. I want the 'from' header to have another my own specified email address whenever user receive email. Could anyone please tell me how can I do this?
MailMessage mailMsg = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(username, password);
MailAddress mailAdd = new MailAddress("jack2#gmail.com");
mailMsg.Sender = new MailAddress(username);
mailMsg.From = mailAdd;
//mailMsg.Headers.Add("Sender",username);
mailMsg.Bcc.Add(builder.ToString());
mailMsg.Subject = txtSubject.Text;
mailMsg.Body = txtBody.Text;
mailMsg.IsBodyHtml = chkHtmlBody.Checked;
if (System.IO.File.Exists(txtAttechments.Text))
{
System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);
mailMsg.Attachments.Add(attechment);
}
client.Send(mailMsg);
In above code 'username' and 'password' fields contain another email address and password. The received email having 'from' header with value

TRY this if your mail provide is not gmail and also not using IMAP services.
MailMessage mailMsg = new MailMessage();
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = "mail.youdomain.com"; //////EDITED
client.EnableSsl = false; //////EDITED
client.Credentials = new System.Net.NetworkCredential(username, password);
MailAddress mailAdd = new MailAddress("jack2#gmail.com");
mailMsg.Sender = new MailAddress(username);
mailMsg.From = mailAdd;
//mailMsg.Headers.Add("Sender",username);
mailMsg.Bcc.Add(builder.ToString());
mailMsg.Subject = txtSubject.Text;
mailMsg.Body = txtBody.Text;
mailMsg.IsBodyHtml = chkHtmlBody.Checked;
if (System.IO.File.Exists(txtAttechments.Text))
{
System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);
mailMsg.Attachments.Add(attechment);
}
client.Send(mailMsg);

Related

Send Email With C# (Cpanel Hosting)

I have bought a cPanel host and the SMTP server information is:
This is my code:
string smtpAddress = "mandane.hostcream.com";
int portNumber = 465;
bool enableSSL = true;
string emailFrom = "mahabadi#exirsec.ir";
string password = Authenitication.PassWord;
string emailTo = To.Text;
string subject = Subject.Text;
string body = Body.Text;
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
When I run my code and click on the send button after 1 or 2 minutes this appears:
Additional information: Failure sending mail.
What am I doing wrong?
I think you missed something, try this:
SmtpClient smtpClient = new SmtpClient();
NetworkCredential smtpCredentials = new NetworkCredential("email from","password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("email from");
MailAddress toAddress = new MailAddress("email to");
smtpClient.Host = "smpt host address";
smtpClient.Port = your_port;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = smtpCredentials;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 20000;
message.From = fromAddress;
message.To.Add(toAddress);
message.IsBodyHtml = false;
message.Subject = "example";
message.Body = "example";
smtpClient.Send(message);
It seems that you cannot reach Yahoo address on port 465, please check if this address reachable first because it appears to be a network issue.

how to send email without go to spam mail

MailAddress mailFrom = new MailAddress("test#smtp.com");
MailAddress mailTo = new MailAddress("tester#gmail.com");
MailMessage mail2 = new MailMessage(mailFrom, mailTo);
mail2.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "xxx.xx.xx.xxx"; // smtp host ip
mail2.Subject = "Testing.";
mail2.Body = "Hello";
mail2.SubjectEncoding = System.Text.Encoding.UTF8;
mail2.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(mail2);
the above is my function that use to send an email via smtp, but I realized all the mail was located in my spam mail folder (Gmail). Is there anyway that can solve it ?
IsBodyHTML is marked true, but you're only providing text/html. You minimally need to include an alternate view with text
mail2.Body = "Hello";
make sure you not using Mail from and mailto is same address or
MailMessage mail2 = new MailMessage(mailFrom, mailTo);
UPDATE
mail2.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "xxx.xx.xx.xxx"; // smtp host ip
mail2.Subject = "Testing.";
mail2.Body = "Hello";
string html = "html";
// here is example to user AlternateViews
mail2.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, new System.Net.Mime.ContentType("text/html"));
string Plaintext ="plain text";
mail2.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(Plaintext, new System.Net.Mime.ContentType("text/plain"));
mail2.SubjectEncoding = System.Text.Encoding.UTF8;
mail2.BodyEncoding = System.Text.Encoding.UTF8;
client.Send(mail2);
Try this and revert.
string mailServer;
int port;
string mailId, mailPass;
string subject;
string mailTo;
subject="something";
StringBuilder mailBody = new StringBuilder();
mailTo = "someone#gmail.com";
mailServer = "smtp.gmail.com";
mailId = "something#gmail.com";
myString.Length = 0;
myString.Append("<html><body><div>BODY CONTENT</div></body></html>");
mailPass = "xxxxxx";
port = 587;
MailMessage mail = new MailMessage(mailId, mailTo, subject, myString.ToString());
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(mailServer, port);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mailId, mailPass);
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);

Using MailMessage to send emails in C#

I am experiencing some problems sending emails with MailMessage. I have two email accounts, (account1#gmail.com, and account2#gmail.com) and I would like account2 to send an email to account one at a button click event.
This is what I have but it's not working. I'm getting and exception saying it's forbidden.
try
{
//do submit
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress("account2#gmail.com", "Account2");
emailMessage.To.Add(new MailAddress("account1#gmail.com", "Account1"));
emailMessage.Subject = "SUBJECT";
emailMessage.Body = "BODY";
emailMessage.Priority = MailPriority.Normal;
SmtpClient MailClient = new SmtpClient("smtp.gmail.com");
MailClient.Credentials = new System.Net.NetworkCredential("account2#gmail.com", "password");
MailClient.Send(emailMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I have a feeling it's a problem with the Smtp but I have no clue.
Try this:
using (MailMessage emailMessage = new MailMessage())
{
emailMessage.From = new MailAddress("account2#gmail.com", "Account2");
emailMessage.To.Add(new MailAddress("account1#gmail.com", "Account1"));
emailMessage.Subject = "SUBJECT";
emailMessage.Body = "BODY";
emailMessage.Priority = MailPriority.Normal;
using (SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587))
{
MailClient.EnableSsl = true;
MailClient.Credentials = new System.Net.NetworkCredential("account2#gmail.com", "password");
MailClient.Send(emailMessage);
}
}
I added the port to the smtp client and enabled SSL.
If port 587 doesn't work, try port 465.
Try:
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(txtUsername.Text);
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
You have not define port number. it should be 587. and use enableSsl=true
as below:
SmtpClient MailClient = new SmtpClient("smtp.gmail.com",587);
Try adding a Port number 587 and enabling SSL on as Gmail uses “strict” SSL security. This means that we’ll always enforce that your other provider’s remote server has a valid SSL certificate.:
try
{
//do submit
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress("account2#gmail.com", "Account2");
emailMessage.To.Add(new MailAddress("account1#gmail.com", "Account1"));
emailMessage.Subject = "SUBJECT";
emailMessage.Body = "BODY";
emailMessage.Priority = MailPriority.Normal;
SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587);
MailClient.Credentials = new System.Net.NetworkCredential("account2#gmail.com", "password");
MailClient.EnableSsl = true;
MailClient.Send(emailMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
MailMessage objMail = new MailMessage();
SmtpClient objSMTP = new SmtpClient("from.google.uk");
MailAddress objTo = new MailAddress("e-mail", "name");
string sql = "Select Naam, Mail from tblMail";
OleDbCommand cmdMail = new OleDbCommand(sql, cnnConnectie);
OleDbDataReader dtrMail = default(OleDbDataReader);
cnnConnectie.Open();
dtrMail = cmdMail.ExecuteReader();
while (dtrMail.Read())
{
objMail.To.Add(dtrMail["Mail"].ToString());
objMail.From = objTo;
objMail.Body = "test";
objMail.Subject = dtrMail["name"].ToString();
objSMTP.Send(objMail);
}
cnnConnectie.Close();

How to send mail using SmtpClient in .net?

I have the following code which is not working:
public static void SendMail(string content, string title, List<string> address)
{
SmtpClient client = new SmtpClient(Server,Port);
client.Port = Port;
client.Host = Server;
client.EnableSsl = false;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(Username, Password);
foreach(string to in address)
{
MailMessage mm = new MailMessage(From, to, title, content);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mm);
}
client.Dispose();
}
I am getting the following error:
Mailbox unavailable. The server response was: You must give your username and password to send mail through this service
You can see that I am passing a username and password. Why am I still getting this issue?
here i am using example of using gmail server
MailMessage mail = new MailMessage();
mail.To.Add(textBox1.Text);
mail.From = new MailAddress("Yourgmailid");
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";
smtp.Credentials = new System.Net.NetworkCredential
("Yourgmailid, "Password");
smtp.EnableSsl = true;
smtp.Send(mail);

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

Categories