Send anonymous emails C# - c#

Hi I want to send password validation to my users using c#, and I wish to protect my mail box getting spammed. How do I do that?
Been trying to this and it's not working:
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.Timeout = 10000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("login", "password");
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress("noreply#mysite.com");
mailMsg.To.Add("user");
mailMsg.CC.Add("cc#ccServer.com");
mailMsg.Bcc.Add("bcc#bccServer.com");
mailMsg.Subject = "Subject";
mailMsg.Body = "BodyOfTheMailString";
smtpClient.Send(mailMsg);
Console.WriteLine("Mail sent");
The user i am sending this email to, getting my gmail account as the sender

This is not C#'s task, neither the body of your message: its your mailbox configuration.
If this is just for email validation, you can always create a new email like "service#domain.com" or "noreply#domain.com" for sending these verifications messages and then set this mailbox to ignore incoming messages.
Also if you try to send messages using emails that are not registered into your server, the server can deny your request.

UPDATE:
You should initially have mentioned that you are using gmail's smtp. To prevent people to send spam gmail always sets from to your emailaddress regardless of what you write in the From property.
Set the From address on the MailMessage to "noreply#mysite.com".
MailMessage mailMsg = new MailMessage();
mailMsg .From = "noreply#mysite.com";
mailMsg .To = "to#toServer.com";
mailMsg .Cc = "cc#ccServer.com"";
mailMsg .Bcc = "bcc#bccServer.com";
mailMsg .Subject = "Subject";
mailMsg .Body = "BodyOfTheMailString";
SmtpMail.Send(mailMsg );

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.

smtp fails to send mail sometimes in c#

I have gone through some questions on this topic. All the answers relate when sending email fails all the time. In my case, it fails only sometimes with exception message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM...
If I try second time it works. I'm using the following configuration.
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("emailid", "displayname");
mail.To.Add("TOAddress");
mail.Subject = subject1;
mail.Body = body1;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient("Outlook.office365.com", 587))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("emailid", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
checked a similar question here , given solutions not working.
Try this(second answer): Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
I used this code to send the email:
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sender#gmail.com");
msg.To.Add("receiver#gmail");
msg.Subject = "Hello";
msg.Body = "Test";
SmtpClient smt = new SmtpClient();
smt.Host = "smtp.gmail.com";
System.Net.NetworkCredential ntcd = new NetworkCredential();
ntcd.UserName = "sender#gmail.com";
ntcd.Password = "senderPassword";
smt.Credentials = ntcd;
smt.EnableSsl = true;
smt.Port = 587;
smt.Send(msg);
Also check if your virus scanner doens't block your email from sending.

How to send mail from Smtp client?

I have an console app and I am trying to send mail from it.
My code.
MailMessage message = new MailMessage(MailSender, "ToMe#me.com");
message.Subject = "Using the new SMTP client.";
message.Body = #"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
try
{
client.Send(message);
}
catch (Exception ex)
{
string t = ex.Message;
}
Got this from here
I must be missing something since I am getting:
Failure sending mail.
What am I doing wrong here?
EDIT:
inner Exeption.
InnerException = {"The remote name could not be resolved:
'smtp.google.com'"}
You can try to use
smtp.gmail.com
instead of
smtp.google.com
Also try to make sure that you are providing the correct credentials along with the correct port. A server parameter info:
Source
So you can try something like this
MailMessage message = new System.Net.Mail.MailMessage();
string fromEmail = "youremailaddress#xyz.com";
string password = "yourPassword";
string toEmail = "recipientemailaddress#abc.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Using the new SMTP client.";
message.Body = "Using this new feature, you can send an e-mail message from an application very easily.";
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, password);
smtpClient.Send(message.From.ToString(), message.To.ToString(), message.Subject, message.Body);
}
#Ra3IDeN ...hey brother try this...
SmtpClient smtpClient = new SmtpClient("mail.yourwebsitename.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("demo#yourwebsitename.com.com", "yourIdPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//code for: From ,CC & To
mail.From = new MailAddress("demo#yourwebsitename.com", "yourwebsite");
mail.To.Add(new MailAddress("demo#yourwebsitename.com"));
mail.CC.Add(new MailAddress("youremailid#gmail.com"));
smtpClient.Send(mail);
If you're trying to send email from a console application (your higher-level problem), I recommend using PostMark. Why:
NuGet - You can get the PostMark NuGet package and send email with a nice API. Convenient and simple.
Not marked as SPAM - You can configure your "server" with verification (including spf and signing). So your email will more likely reach the destination in their inbox rather than their SPAM box.
Free - to a point. I think it's 1000 emails for free then $1 per 1000. So that's pretty good. Compare that to any other vanilla SMTP server for rent. PostMark is cheap
Consistent - From Workstation DEV to server LIVE, the PostMark API is consistently accessible. I cannot stress how good that is. Often a server host will offer SMTP server endpoint but it will only work from inside their network, meaning you have to configure another SMTP server when you're doing DEV work on your workstation (or it simply wont work).
Async Interface - I'm not sure if built-in smtp client in .Net is async...
Tracking - Hey look at that, they have a tracking feature built-in. That's snazzy.
Example code for sending (source):
var message = new PostmarkMessage()
{
To = "recipient#example.com",
From = "sender#example.com",
TrackOpens = true,
Subject = "A complex email",
TextBody = "Plain Text Body",
HtmlBody = "<html><body><img src=\"cid:embed_name.jpg\"/></body></html>",
Tag = "business-message",
Headers = new HeaderCollection{
{"X-CUSTOM-HEADER", "Header content"}
}
};
var imageContent = File.ReadAllBytes("test.jpg");
message.AddAttachment(imageContent, "test.jpg", "image/jpg", "cid:embed_name.jpg");
var client = new PostmarkClient("server token");
var sendResult = await client.SendMessageAsync(message);
MailMessage msg = new MailMessage("YourEmail#gmail.com", "DestinationEmail#something.com");
msg.Subject = message.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Message Content here as HTML", null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32( 587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("YourEmail#gmail.com", "YourPassword");
smtpClient.EnableSsl = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
smtpClient.Credentials = credentials;
smtpClient.Send(msg);

sending mail through gmail smtp from .net

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

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