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
}
}
Related
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.
I am using SmtpClient to send email like this
MailMessage mail = new MailMessage("email-from#gmail.com", "email-to#gmail.com", "Contact from " + emailFrom, text);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(emailAccount, accountPass);
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(mail);
This is working fine, I am receiving emails but I receive email from emailAccount address variable. I want to recieve emails from the 'From' field of message i.e email-from#gmail.com
Is it possible?
You can do something like these.
mail.From = fromEmailAddress; // Your variable that contains from address
for more details see.
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.110).aspx
Sending Email to multiple Recipients with MailMessage
http://www.c-sharpcorner.com/article/smtpmail-and-mail-message-send-mails-in-net/
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.
In C# I have a method that sends an email via a gmail account.
When I open the email in microsoft outlook the from address is shown as the gmail address and not the strFromAddress that I use in the headers.
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential("***#gmail.com", "*****");
var strFromAddress = "no-repl#demuynck-printing.be";
var strToAddress = "sander#demuynck-media.be";
var strSubject = "Album: '" + lbltitel.Text + "' bestelling";
var strBody = "<html><head>";
// new instance of MailMessage
MailMessage mailMessage = new MailMessage();
// Sender Address
mailMessage.From = new MailAddress(strFromAddress);
// mailMessage.Headers("Selexion Clix Demuynck <no-reply#demuynck-printing.be>");
// mailMessage.Bcc.Add(new MailAddress("no-reply#demuynck-printing.be"));
// Recepient Address
mailMessage.To.Add(new MailAddress(strToAddress));
mailMessage.Headers.Add("Reply-To", "info#demuynck-printing.be");
// Subject
mailMessage.Subject = strSubject;
// Body
mailMessage.Body = strBody;
// format of mail message
mailMessage.IsBodyHtml = true;
// new instance of Smtpclient
smtp.Send(mailMessage);
Just set the displayname property on the MailAddress like so:
MailAddress fromAddress = new MailAddress("user#domaina.com","no-reply#domainb.com");
GMail will change the 'from' address to the account used to login to the SMTP server, unless the email-address used in 'from' field is verified to belong to the same owner. So, in your gmail preferences, just add and verify this specific from address.
This happens because Google is violating the SMTP protocols. There is a detailed article about it here: http://lee-phillips.org/gmailRewriting/
Steve's comment is incorrect; an authenticated sender should be able to set any From: header.
For my application I have code to send emails for live/hotmail, but not for gmail, that doesn't work I tried to build a check for it, to see what account is used to send the e-mail but it's not working, when I try to send an Gmail email. Here is the code I use for the check:
MailMessage msg = new MailMessage();
msg.To.Add(txtAan.Text);
msg.From = new MailAddress(txtGebruikersnaam.Text);
msg.Subject = txtOnderwerp.Text;
msg.Body = txtBericht.Text;
string smtpcheck = txtGebruikersnaam.Text;
smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));
SmtpClient smtp = new SmtpClient();
if (smtpcheck.ToLower() == "#gmail.com")
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
}
else if(smtpcheck.ToLower() != "#gmail.com")
{
smtp.Host = "smtp.live.com";
smtp.Port = 587;
}
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential(txtGebruikersnaam.Text, txtWachtwoord.Text);
smtp.Send(msg);
This code gives me an error when I try to send an E-mail by using Gmail, can somebody give me a little help with this problem? And yes I also tried the port: 465 and 587 for gmail, so I don't think that is the problem either.
This line doesn't change the value of smtpcheck
smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));
you need to write
smtpcheck = smtpcheck.Substring(Math.Max(0, smtpcheck.Length - 10));
As result your if condition fails and you fall to send the mail always with live.com
EDIT: For gmail, this code is confirmed to work
SmtpClient sc = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("username", "password");
sc.UseDefaultCredentials = false;
sc.Credentials = nc;
sc.EnableSsl = true;
sc.Port = 587;
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}