I'm using Mailkit in Asp.net Core to receive email from other but To and From email is the same but in debug To and From have the correct emails?
This is my code what's wrong?/?
MimeMessage emailMessage = new MimeMessage() ;
emailMessage.From.Add(MailboxAddress.Parse(_userIdentity.Email));
emailMessage.To.Add(MailboxAddress.Parse("my email"));
emailMessage.Subject = "Support email";
BodyBuilder emailBodyBuilder = new BodyBuilder();
emailBodyBuilder.TextBody = message;
emailMessage.Body = emailBodyBuilder.ToMessageBody();
var smtp = new MailKit.Net.Smtp.SmtpClient();
smtp.Connect("smtp.gmail.com" , 587, SecureSocketOptions.StartTls);
smtp.Authenticate("my email", "*********");
await smtp.SendAsync(emailMessage);
smtp.Disconnect(true);
Let me guess. You are setting the From address to an address that does not match your #gmail.com address.
When you send the message, the From address gets replaced with your #gmail.com address.
This is because smtp.gmail.com rewrites the From header in order to prevent you from spoofing another person's email address.
Related
I am using MVC framework 4.5 C# and publish the my project on Windows server 2012 R2. In that server when I tried to sending mail with gmail but its can not send the mail and giving the below description.
The SMTP server requires a secure connection or the client was not
authenticated.The server response was: 5.5.1 Authentication Required.
I installed SMTP Service and all configuration regarding that.
Same mail configuration is running my development server.
It is due to security check on gmail so please follow below steps.
Log in to production server via remote access, and sign in to gmail once with your credentials. They will ask for the confirmation, confirm it
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = new System.Net.NetworkCredential("your email address", "password");
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
// Specify the email sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("your email address");
// Set destinations for the email message.
MailAddress to = new MailAddress(textBox_SedToEmail.Text);
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test email message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Attachments.Add(new Attachment(#"C:\Users\eddie\Pictures\2.jpg"));
// Set the method that is called back when the send operation ends.
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.SendAsync(message, userState);
I am attempting to send an email using the SendGrid library: https://github.com/sendgrid/sendgrid-csharp
More specifically I am doing this...
// Create the email object first, then add the properties.
SendGridMessage myMessage = new SendGridMessage();
myMessage.AddTo("anna#example.com");
myMessage.From = new MailAddress("john#example.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("username", "password");
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
transportWeb.DeliverAsync(myMessage);
The code runs fine however I never receive an email!! How do I diagnose this problem?
Note the email doesn't appear in the Bounces/Blocks/Spam Reports or Invalid Email lists.
I know that this is an old post, but here's my answer:
SendGrid is not used for receiving emails to your email address, only to send them in mass. If you want to use I suggest using SmtpClient from MailKit. Be sure to use the proper hostname, like smtp.gmail.com, a port number, like 465 for SSL, and the proper credentials (Username and password of any email address).
One thing to note, if you use your own credentials for your own email address and you want to send it to that same email address, like if you want to send an email to JoeBlank#email with it's own credentials, you will only send to yourself. If you want to reply back to a different email address, fill in the ReplyTo field. The idea is to let the email of the authorized email address send you the message. Here's the code:
using MimeKit;
using MailKit.Net.Smtp;
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender Name", "sender#example.com"));
message.To.Add(new MailboxAddress("Receiver Name", "receiver#example.com"));
message.ReplyTo.Add(new MailboxAddress("ReplyTo Name", "replyto#example.com"));
message.Subject = "Subject";
message.Body = new TextPart("plain")
{
Text = plainTextContent
};
using (SmtpClient smClient = new SmtpClient())
{
smClient.Connect("smtp.gmail.com", 465, true);
smClient.Authenticate("user", "pass");
await smClient.SendAsync(message);
smClient.Disconnect(true);
}
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
smtpClient.Credentials = new NetworkCredential("xxxxx#gmail.com", "password");
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//mail.From = new MailAddress("xxx1#gmail.com");
mail.To.Add("XX2#gmail.com");
mail.Subject = "Test mail";
mail.Body = "This is test mail, with test content";
smtpClient.Send(mail);
In the above code, why mail.From is mandatory? even I specified mail-id in smtpClient.Credentials.
Even I specified mail.From with some mail-id, the receiver is not receiving mail from this mail address, instead receiving from xxxxx#gmail.com which I specified in smtpClient.Credentials.
From is supposed to be the address from where the mail was sent.
This can be different from your Smtp credentials. Some servers will let you have a username that is different from your mail address.
Gmail shows the email address from where it received the mail.
They do that so that users don't mistake getting the mail from someone who didn't really send it.
Not all email clients do that.
You cannot fake a from address to gmail. They will always show where they got the email from.
How to send the email through mine company email id
Like every company use to give some email id to its associates like xyz#abc.com
How to send the mails through this id to other accounts. Also I don't have passwords for this id, then how to send the email in this situation.
You could use the SmtpClient class. Other than the from and to addresses you will need your company's SMTP server:
var message = new MailMessage();
message.To.Add("to#abc.com");
message.Subject = "This is the Subject";
message.From = new MailAddress("from#abc.com");
message.Body = "body of the mail";
using (var smtpClient = new SmtpClient("smtp.abc.com"))
{
smtpClient.Send(message);
}
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)