Not receiving email using SendGrid - c#

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);
}

Related

How do I call a method to automatically send an email in the controller?

I need to be able to send automatic emails to any users who have registered new accounts, changed passwords, and/or created new orders.
I've been given the SendEmail file, which belongs in the "Utilities" folder in my solution.
using System;
using System.Net.Mail;
using System.Net;
namespace SendEmail
{
public static class EmailMessaging
{
public static void SendEmail(String toEmailAddress, String emailSubject, String emailBody)
{
//Create a variable for YOUR TEAM'S Email address
//This is the address that will be SENDING the emails (the FROM address)
String strFromEmailAddress = "email#gmail.com";
//This is the password for YOUR TEAM'S "fake" Gmail account
String strPassword = "Password";
//This is the name of the business from which you are sending
//TODO: Change this to the name of the company you are creating the website for
String strCompanyName = "Team Final Project";
//Create an email client to send the emails
//port 587 is required to work, do not change it
var client = new SmtpClient("smtp.gmail.com", 587)
{
UseDefaultCredentials = false,
//This is the SENDING email address and password
//This will be your team's email address and password
Credentials = new NetworkCredential(strFromEmailAddress, strPassword),
EnableSsl = true
};
//Add anything that you need to the body of the message
//emailBody is passed into the method as a parameter
// /n is a new line – this will add some white space after the main body of the message
//TODO: Change or remove the disclaimer below
String finalMessage = emailBody + "\n\n Thank you, come back again soon!";
//Create an email address object for the sender address
MailAddress senderEmail = new MailAddress(strFromEmailAddress, strCompanyName);
//Create a new mail message
MailMessage mm = new MailMessage();
//Set the subject line of the message (including your team number)
mm.Subject = "Team ## - " + "Thank you!";
//Set the sender address
mm.Sender = senderEmail;
//Set the from address
mm.From = senderEmail;
//Add the recipient (passed in as a parameter) to the list of people receiving the email
mm.To.Add(new MailAddress(toEmailAddress));
//Add the message (passed)
mm.Body = finalMessage;
//send the message!
client.Send(mm);
}
}
}
My problem is that neither I nor my team members know how to implement call this from the controller in a way that will be sent automatically and with the user's email and name. We imagine they will be in the Account and Orders controllers. The accounts controller has the register and change password methods, which work, and the orders controller has the complete order method.
Also, we are not using a confirmation view, it has to be an automatic email.
We need some direction in figuring out where exactly we need to call the method from and how.
The most helpful thing I've found on the internet today is this block of code for a test message that is not intended to be sending automatic emails.
public static void CreateTestMessage(string server)
{
MailAddress from = new MailAddress("sender#gmail.com", "Team Project");
MailAddress to = new MailAddress("reciever#gmail.com", "Customer");
MailMessage message = new MailMessage(from, to);
message.Subject = "Password Changed";
message.Body = #"This is a confirmation email that your password has been changed.";
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString());
}
}
Everything is being coded on MS VS
First create a service for EmailService and put SendEmailAsync Method in it
and call this method on your Controller.
In Method body:
1-Create your message:
var mm = new MailMessage()
2-Then you should build your smtp:
using var smtpClient = new SmtpClient();
3-Then connect it to your server
await smtpClient.ConnectAsync(
"your host",
"port",
SecureSocketOptions.SslOnConnect);
4-Now send your Email:
await smtpClient.SendAsync(mm);
5-Make sure that disconnect from your client:
await smtpClient.DisconnectAsync(true);
Note: It may give you an Exception while Connecting or Sending your Email so don't forget try catch block.
6-For Automation you can use a EmailAccount Table with relation with your Customer Table and keep your message data in it.
for ex: body ,subject..... .
In your Contorller you have your Customer So you can get his EmailAccount from DataBase and pass EmailAccount Entity to SendEmailAsync Method.
instead of creating message in body, get it from EmailAccount Entity and then countinue the Steps.
7-Enjoy it :)

How to send an email with Mailkit

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.

Set a sender different from smtp client user

I must send different emails... Every email has a aits own sender.. I want to connect to my smtp server just with one account..
So, for example I want to connect to the smtp server with this user:
smtpclient#something.com
But I want to send the email from
noreply#something.com
I wrote some code that send the email.. the code works because I receive the email.. but something goes wrong:
I receive the email from smtpclient#something.com but I would like to receive the email from noreply#something.com
I am using EmailDefinition:
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = urlEmailLayout;
mailDefinition.IsBodyHtml = true;
mailDefinition.From = from.EmailAddress;
MailMessage email = mailDefinition.CreateMailMessage(string.Join(",", to.Select(t => t.EmailAddress)), bodyValues, new System.Web.UI.LiteralControl());
email.From = new MailAddress(from.EmailAddress, from.DisplayName);
And to send the email I use SmtpClient:
SmtpClient client = new SmtpClient(smtpServer.Host, smtpServer.Port);
client.EnableSsl = smtpServer.RequireCredential;
if(smtpServer.RequireCredential)
client.Credentials =
new System.Net.NetworkCredential(
smtpServer.Credential.Username,
smtpServer.Credential.Password
);
client.Send(this._email);
How can I do?
Thanx
I don't think it works like that. Whatever credentials you use for your SMTP connection is the address it gets sent from.
Why not just connect to the SMPT server using the credentials for noreply#something.com?

Not able to send mail to group email id

I'm using smtp (c#)and trying to send mail to group id(official id) but its not able to send mail.Although through same code I'm able to send mail to individual id. Any idea what could be wrong here.
Following code I'm using
MailMessage mail = new MailMessage();
mail.To = "group#company.com";
mail.From = "me#company.com";
mail.Subject = "Test Mail: please Ignore";
mail.Body = body;
SmtpMail.SmtpServer = "mailhub.int.company.com";
SmtpMail.Send(mail)
I'm getting following error in my mail box:
Delivery has failed to these recipients or distribution lists:
group#company.com
Not Authorized. You are not authorized to send to this recipient or distribution list. For distribution lists please check approved senders in the corporate directory.
_____
Sent by Microsoft Exchange Server 2007
Diagnostic information for administrators:
Generating server: GSPWMS005.int.company.com
group#company.com
#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##
From error I could get an idea that some authentication is missing but not sure which one or how to resolve it.
If I send mail to this group thorough my outlook then its working ok.
http://msdn.microsoft.com/en-us/library/59x2s2s6.aspx
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
message.Subject = "Test Email using Credentials";
NetworkCredential myCreds = new NetworkCredential("username", "password", "domain");
CredentialCache myCredentialCache = new CredentialCache();
try
{
myCredentialCache.Add("ContoscoMail", 35, "Basic", myCreds);
myCredentialCache.Add("ContoscoMail", 45, "NTLM", myCreds);
client.Credentials = myCredentialCache.GetCredential("ContosoMail", 45, "NTLM");
client.Send(message);
Console.WriteLine("Goodbye.");
}
catch(Exception e)
{
Console.WriteLine("Exception is raised. ");
Console.WriteLine("Message: {0} ",e.Message);
}

Sending Mails from Oulook to Other accounts

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);
}

Categories