send mail when Anonymous Authentication for SMTP server - c#

// 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.

Related

Send email in ASP.net without password

I may be Wrong but I am stuck in an issue...
I want to send the email to the user using ASP.net without having the password
HERE is my code
But I do not have the password .How can I send email to the user without having their password.
protected void submit_Click(object sender, Event Args e)
{
MailMessage mail = new MailMessage();
mail.To.Add(txtTo. Text);
mail.From = new Mail Address(txt_from.Text);
mail.Subject = txtSubject. Text;
mail.Body = txtBody.Text;
mail.IsBodyHtml = true;
if (fileUpload1.HasFile)
{
mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Host = "mysmtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential("email", "password");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
You don't need their password to send email. You need password to your stmp server. You have to sing in to it to send any mail. Try to create an email address for test puproses and use it credentials to send email.
There is part which you should fix for gmail stmp server:
smtp.Host = "smtp.gmail.com";
stmp.Port = 465;
stmp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential
("example#gmail.com", "exampleGmailPassword");

Problems sending e-mail in web app

I have been trying for 2 days to get my ASP.NET webforms application to send an e-mail.
I have tried this using both outlook and gmail. I got the smtp information for both from this tutorial:
When I try using port 587 in the example below I get an error saying:
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
When I try using port 465 in the example below:
My application just hangs forever and my page never gets back my email function which is in the PageLoad.
A couple of things to note just incase one of these is messing me up:
I am using a standard VS2013 dev environment (running my web app in debug mode using F5)
I am using the same e-mail address for from, to, and gmail login credentials)
My ISP does not block these ports. (comcast cable)
I even went to the google DisplayUnlockCaptcha page to Allow access to my account
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
using (MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress))
{
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
}
This code should work fine for you
protected void SendEmail()
{
string EmailAddress = "myemail#gmail.com";
MailMessage mailMessage = new MailMessage(EmailAddress, EmailAddress);
mailMessage.Subject = "This is a test email";
mailMessage.Body = "This is a test email. Please reply if you receive it.";
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = EmailAddress,
Password = "password"
};
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(mailMessage);
}
You'll need to set the delivery mode otherwise gmail will return an error
EDIT:
Throwing an 'using' around 'MailMessage' might also be a smart thing to do
It turns out that it is because of a GMail security setting.
https://www.google.com/settings/security/lesssecureapps
You have to enable access for less secure apps.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("xxx#gmail.com");
mail.From = new MailAddress("yyy#gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yyy#gmail.com", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}``
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/

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

Send anonymous emails 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 );

Categories

Resources