Set a sender different from smtp client user - c#

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?

Related

C# Sending Email using SMTP E-mail

I'm rather new to SMTP and IIS settings but according to the documentation on I've read the web this should be working.
What I'm trying to achieve:
To send an email from the server to a users email using an existing SMTP Relay Server.
What I have done:
In my IIS, for my site (ASP.NET), I have configured the SMTP E-mail.
I have entered:
A random E-mail address (it doesn't have to be an existing, right?)
A SMTP Server IP (in this case an IP to an external SMTP Relay Server)
A port number (25).
Autentication Settings to "Not required".
My method for sending an email looks like this:
public static void SendEmail()
{
var message = new MailMessage()
{
Subject = "Heading",
Body = "Body",
message.From = new MailAddress("test#test.com");
message.To.Add("A valid email address"); //My own email address
}
var smtpClient = new SmtpClient("SMTP-Relay-Server-IP", 25); //Same IP as the one in SMTP E-mail configuration in IIS for the site.
smtpClient.Send(message);
}
}
Facts/questions:
Is this correct? Is it correct to also put the Relay Server IP and Port number in the code as parameters in the new SmtpClient?
I don't get an error but I don't receive an email. (I am 100% sure that the "to-email" is correct.
What can be the reason for this not working? What am I missing or misunderstanding?
Wrap your smtpClient.Send(message); in a try/catch block and log any exceptions that are thrown.
A random E-mail address (it doesn't have to be an existing, right?)
That depends on your SMTP provider and configuration.
Without more information on your SMTP provider or an error message I doubt there's anything we can do for you.
MailMessage mail = new MailMessage("sendTo", "from");
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("user", "pass");
smtp.Port = 25;
smtp.EnableSsl = true;
smtp.Host = "smtp.gmail.com";
mail.Body = "this my message";
smtp.Send(mail);
should be set out like this

Not receiving email using SendGrid

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

Email not working,Not able to receive email for yahoo email id

I am stuck and not able to receive email from yahoo id(if the sender email id is of yahoo).
code is working fine not giving me any error and i am receiving email fromm gmail id.
i am using localhost (not in local machine on live server).
hosting server : smtp.snapgrid.com
also used authentication,enable ssl , using proper port for ssl.
on snapgrid i do check so i got is mail from yahoo is blocked and the message is,
message :
Type: blocked
Reason: 550 5.7.1 Unauthenticated email from yahoo.com is not accepted due to domain's
DMARC policy. Please contact administrator of yahoo.com domain if this was a legitimate
mail. Please visit http://support.google.com/mail/answer/2451690 to learn about DMARC
initiative.
please help...
code i used to send is(its working fine just giving for idea):
Method 1:
SmtpClient objSMTPClient = new SmtpClient();
objSMTPClient.Host = ConfigurationManager.AppSettings["strSMTPServer"];
string BODY_FORMAT = ConfigurationManager.AppSettings["EmailBodyContentFormat"];
MailMessage objMailMessage = new MailMessage(from.Trim(), to.Trim(), subject.Trim(), body.Trim());
objSMTPClient.UseDefaultCredentials = false;
if (BODY_FORMAT.ToUpper() == "HTML")
objMailMessage.IsBodyHtml = true;
else if (BODY_FORMAT.ToUpper() == "TEXT")
{
body = StripTags(body);
objMailMessage.IsBodyHtml = false;
objMailMessage.Body = body.ToString().Trim();
}
else
return false;
objSMTPClient.Send(objMailMessage);
return true;
Method 2:
SmtpClient oMail = new SmtpClient();
MailMessage msg = new MailMessage();
MailAddress Madd = new MailAddress(from, "sunil");
oMail.Host = "smtp.gmail.com";
oMail.Port = 587;
oMail.EnableSsl = true;
oMail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
oMail.Credentials = new NetworkCredential("sunil123#mydomain.com", "******");
oMail.Timeout = 20000;
msg.From = Madd;
msg.Body = body.ToString();
msg.To.Add(to);
msg.Subject = subject;
msg.IsBodyHtml = true;
oMail.Send(msg);
return true;
both are working having no bug running without error....
If you are sending via a server belonging to someone like Yahoo, Google or Office365 they expect the sender name of the account to match that that you're sending using in the from address.
For example, this would work on a your local SMTP server:
Message.From = new MailAddress("GrandMasterFlush#domain.com");
However, to get it to send via someone like Yahoo would require you to send it like this:
Message.From = new MailAddress("GrandMasterFlush#domain.com", "Grandmaster Flush");
If the sender name provided does not exactly match that of the account the email will not get sent.

SMTP Service not available

I am trying to create a web application which upon entering your email address and message , sends an email with this information from the email address.
I used this:
try
{
NetworkCredential login = new NetworkCredential("your_____#gmail.com", "password");
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.To.Add(new MailAddress("my____email#gmail.com"));
email.From = new MailAddress("your_____#gmail.com");
email.Subject = "Question";
email.Body = question;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = login;
client.Send(email);
}
catch
{
}
But its giving me an SMTP error.
"Service not available, closing
transmission channel. The server
response was: Cannot connect to SMTP
server 209.85.129.111
(209.85.129.111:25), connect error
10051" System.Exception
{System.Net.Mail.SmtpException}
To send through your gmail account, you need to connect to port 587:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
You do not need to specify port 587 - the code works without it. I have successfully sent and received e-mail using:
SmtpClient client = new SmtpClient("smtp.gmail.com");
If you look at the error closely, it says "Cannot connect to SMTP server" and error 10051 means the network is unreachable. Do you have a firewall blocking port 587?
Gmail uses port 465 and the erros show port 25
try using 465 port
http://mail.google.com/support/bin/answer.py?answer=76147

Can for loop be used to send bulk mails, Bulk mail Issues, AWS(amazon) SES

I am using C# and .net for coding, to send bulk mails around 5000 mails at one shot using AWS(AMazon) SES(Simple Email Service) API, Everything is working fine if the number of mails sending are less than 500-600(approximately), but if it is more like 5000 it will send upto 500-600 and then it will stop sending emails. I have used datatable to store the mails list from database, assigned template body and subject, and then used for loop to send emails one by one. I need to know whether it is coding problem or for loop issue or some other thing ? Any suggestion is also helpful to me?
for (i = 0; i < dtable1.Rows.Count; i++)
{
Object a=dtable1.Rows[i]["student_emailid"];
String TO1=Convert.ToString(a);
TO1 = TO1.Trim();
if (TO1.Equals("")) {
continue;
}
const String FROM = "asit#amcsquare.com"; // Replace with your "From" address. This address must be verified.
String TO = TO1; // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
String SUBJECT = email_subject;
String BODY = templateBody;
// Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
const String SMTP_USERNAME = "XXXXXXXXXXXX"; // Replace with your SMTP username.
const String SMTP_PASSWORD = "XXXXXXXXXXXXXXXXXX"; // Replace with your SMTP password.
// Amazon SES SMTP host name. This example uses the us-west-2 region.
const String HOST = "email-smtp.us-east-1.amazonaws.com";
// Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
// STARTTLS to encrypt the connection.
const int PORT = 587;
// Create an SMTP client with the specified host name and port.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
//Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
//the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.Send(message1);
}
}
I don't know the exact reason why it is working now, but it's working. I changed the logic of the above code, it started working. Instead of fetching the SMTP connection each time, for sending each mail previously, this time I fetched the smtp connection only once and used it to send all the bulk mails at once and it started working.But the problem is the sending time, it is taking too much to send all the mails.Anyways I will find the solution for this also.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
client.EnableSsl = true;
for(i=0;i<mail.Count;i++)
{
String TO = mail[i];
System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage(FROM, TO, SUBJECT, BODY);
message1.IsBodyHtml = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message1);
}
client.Dispose();
}
Label1.Text = mail.Count.ToString() + " mails sent !!";
}

Categories