sending mail through gmail smtp from .net - c#

I have a few domains set up on the gmail servers through a few different accounts.
I can successfully send mail using the following code from one of the accounts (using the credentials I use to log into the cpanel), but when I try and send mail from one of the user accounts (using that user's credentials), nothing goes out.
Is there a setting I need to set on the gmail side to enable this?
here is the code that works but from only the "parent" account on one of my domains:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("someaddress#somedomain.com", "somepassword");
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someuser#someotherdomain.com"));
msg.Subject = "Inquiry from blah blah blah";
msg.IsBodyHtml = true;
msg.Body = "blah blah blah";
msg.From = new MailAddress("someaddress#somedomain.com");
client.Send(msg);

When you are trying to send mail frim gmail servers, you must use gmail credentials not your domain credentials.
for example use someaddress#gmail.com & your gmail password.
MailMessage mail = new MailMessage();
mail.To.Add("recipient#yahoo.com");
mail.From = new MailAddress("sommeemail#gmail.com", "sender name");
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("sommeemail#gmail.com","password");
smtp.EnableSsl = true;
smtp.Send(mail);

Related

send an email from office 365 as from address not same as Credential Address

I want to send an email in ASP.net where I want to hide the email address that I send to the customers. I want to show them a different email address in the From box so for e.g. I want to send an email from abc#abc.com, this email is authenticated. When the end user receives the email in their inbox, they will see a different address called test#test.com. They can reply to this email. I have this code so far:
testMessage msg = new MailMessage();
msg.To.Add(new MailAddress(txtemail.Text.Tostring(), "To Name"));
msg.From = new MailAddress("abc#abc.com", "From Name");
msg.Subject = "This is a test";
msg.Body = "Hello, this is a test email";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("userName", "PASS");
client.Port = 121;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
I don't want the customers to see my abc#abc.com email address. Instead of that, I want them to see Test#test.com. How can I achieve this? They can reply to test#test.com. I don't want them to reply to abc#abc.com. I am not sure which settings should I change to do this?
Any help will be highly appreciated.

send mail when Anonymous Authentication for SMTP server

// Send anonymous e-mail using SMTP Server (c# code )
//setting SmtpClient property
SmtpClient smtpClient =new SmtpClient();
smtpClient .Host = "smtp.gmail.com";
smtpClient .Port = 587;
smtpClient .EnableSsl = true;
smtpClient .DeliveryMethod =SmtpDeliveryMethod.Network;
smtpClient .UseDefaultCredentials = false;
smtpClient .Timeout = 30000;
//setting MailMessage property
MailMessage mailMessage = new MailMessage();
mailMessage.Subject = "test mail";
mailMessage.From = (new MailAddress("abc#gmail.com"));
mailMessage.To.Add("xyz#gmail.com");
mailMessage.Priority = MailPriority.High;
mailMessage.Body = "Hello";
smtpClient.Send(mailMessage);
Basically, I'm trying to send mail when Authentication is Anonymous. if I pass username and password then e-mail send successfully but if I remove username and password credential (i.e Authentication is Anonymous) then I fail to send mail.

Sending a gmail email

I am trying to send an email through and C# application (Console). Actually I would like it to send an email to any type of email but for the time being if I can get it to send to a gmail account I would be happy enough.
I just want to be able to get this to send to a gmail account for the time being?
Whole Program:
namespace EmailAddress
{
class Program
{
static void Main(string[] args)
{
Program test = new Program();
test.email_send();
}
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("hg#gmail.com");
mail.To.Add("hg#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:/example1.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail#gmail.com", "your password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
}
}
New code: Doesn't hang but will not send the message to the inbox
static void Main(string[] args)
{
Program test = new Program();
//test.email_send();
test.CreateTestMessage4();
}
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("g#gmail.com");
mail.To.Add("g#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
mail.Attachments.Add(attachment);//list of attachements
smtp.Port = 587;//google standard -- most of the time wouldn't not set
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("*","*");
smtp.Send(mail);
Console.WriteLine("-- Sending Email --");
Console.ReadLine();
}
Can someone try this code and see if it works. For some reason this isn't working so I would like to have someone's fresh perspective of this. I just call this in the main method for an instance of the class.
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your email address");
mail.To.Add("your email address");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
mail.Attachments.Add(attachment);//list of attachements
//smtp.Port = 587;//google standard -- most of the time wouldn't not set
//smtp.EnableSsl = true;
//smtp.UseDefaultCredentials = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//smtp.Credentials = new System.Net.NetworkCredential("your address",
"your password");
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("*", "*"); smtp.Send(mail);
Console.WriteLine("-- Sending Email --");
Console.ReadLine();
}
Your code is close:
MailMessage mail = new MailMessage();
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) {
mail.From = new MailAddress("haufdoug#gmail.com");
mail.To.Add("haufdoug#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "Your attachment is accessible on your computer!";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
mail.Attachments.Add(attachment);
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("your mail#gmail.com", "your password");
smtp.Send(mail);
}
Now, regarding some of your other questions.
Port 587 is used by Google only. Typically the mail server you connect to will tell you what port to use for SMTP mail; Google said there's is 587.
For sending through other servers you typically won't set the port number.
side notes:
SmtpClient implements the IDisposable interface. So it should be wrapped in a using clause to ensure it is properly disposed of when complete. Unless you are using the .SendAsync() method.
I changed the variable name from SmtpServer to smtp for two reasons. First, naming convention. Variables inside a method are recommended to be camel case (forExampleThis). Second, SmtpServer is a misnomer as the object is an SmtpClient. Those are very different things and it's just bad practice to misname things.
Can you try this, it's working for me:
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Port = 587;
SmtpServer.Host = smtp.gmail.com;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail#gmail.com", "your password");

Error in Sending Email via a SMTP Client

This may be very trivial for you but i just couldn't figure out why am i getting this error message when i run my code. I looked some of the relative questions on this same website for eg Sending email through Gmail SMTP server with C#
but none of them was helpful. Anyone willing to help please?
using different assemblies are also acceptable. so if anyone got a working solution that would be appreciated.
Error Message = The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
here is my code
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("bob#googlemail.com");
message.To.Add("bob#hotmail.com");
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("MyGoogleMailAccount",
"mygooglemailpassword");
smtpClient.Send(message.From.ToString(), message.To.ToString(),
message.Subject, message.Body);
I don't think there's anything wrong with your code other than the e-mail addresses. I used this code to successfully send an e-mail from gmail to my personal account (ran it in LINQPad, actually). Simply replace the 3 string values with valid values for your accounts and you should be good to go:
MailMessage message = new System.Net.Mail.MailMessage();
string fromEmail = "myaddr#gmail.com";
string fromPW = "mypw";
string toEmail = "recipient#receiver.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);
smtpClient.Send(message.From.ToString(), message.To.ToString(),
message.Subject, message.Body);
}
By this post.
MailMessage mail = new MailMessage("you#yourcompany.com", "user#hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

While receiving mail it show "admin#gmail.com" in from box,but it should show "from#gmail.com"

Sample Code: I need functionality for send to your friend...
NetworkCredential loginInfo = new NetworkCredential("admin#gmail.com", "password");
MailAddress to = new MailAddress("mailto#gmail.com");
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.Credentials = loginInfo;
MailAddress from = new MailAddress("from#gmail.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "demo";
message.Body = #"msgBody";
client.Send(message);
Reasion is are you using smtp of Gmail.
it would happend due to security issule like... you are using gmail address and send email as xyz#microsoft.com its may create issue....
thats reasion its not allow by gmail.
it may work with your own domain.
You can try this
message.Headers.Add("From", "From Name <from#gmail.com>");

Categories

Resources