Send email in ASP.net without password - c#

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

Related

How to send an email from any Email Address in ASP.Net/C# Webforms

I am a beginner programmer in C#, I want to create a contact form , where the user can enter any email address as the "From" address, and they can send a message. Every time it sends, it says the "From" address is the credentials that I am using. How do I change the From address to the address based on the user's input like any other contact form. Should I use a different Smtp Server?
Thanks
protected void sendbttn_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(fromtxt.Text);
message.To.Add("toEmail");
message.Subject = "Subject: " + subjecttxt.Text;
message.Body = mesgtxt.Text;
message.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("Credemail", "Credpassword");
smtpClient.EnableSsl = true;
smtpClient.Send(message);
MessageBox.Show("Your email has been sent!");
fromtxt.Text = "";
subjecttxt.Text = "";
mesgtxt.Text = "";
}
catch
{
MessageBox.Show("Something went wrong, please try again");
}
If your host is not registered as official mail provider, you should never even think about sending E-Mail in the name of somebody else.
Instead use a dedicated address as sender and to authenticate at the smart host and remember to store the credentials in a save location so nobody can "adopt" them - the program sourcecode as in the example is definitely not a save place, store them in IIS property fields.
The address entered in the form may be used as reply-to and possibly as the senders display name.
The body will not be html, too. The property therefore should be 'false'.
string credMail = "example#gmail.com";
string credPasswd = "example_p#asswd";
MailMessage message = new MailMessage();
message.From = new MailAddress(credMail,fromtxt.Text);
message.To.Add("toEmail");
message.Subject = "Subject: " + subjecttxt;
message.Body = mesgtxt.Text;
message.IsBodyHtml = false;
message.ReplyToList.Add(fromtxt.Text);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(credMail, credPasswd);
smtpClient.EnableSsl = true;
smtpClient.Send(message);
MessageBox.Show("Your email has been sent!");

SMTP Mail with ASP.net Error 5.5.1 Authentication Required

Good day, I'm a beginner from using ASP.net and SMTP Mailer
Heres my Question, I always encounter this Error when i send email from my local
and searched and tried the solutions around the net but not so lucky,
I hope someone point out what codes do i need and where i encounter this errror
Message = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
Heres my Code:
protected void btnSendEmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Value, txtName.Value);
smtpClient.Credentials = new System.Net.NetworkCredential("myUser#gmail", "password");
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true;
//Default port will be 25
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("myEmail#gmail.com");
message.Subject = txtSubject.Value;
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1#yoursite.com")
message.CC.Add("myEmail#gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("myEmail#gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtaMessage.Value;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.HeadersEncoding = System.Text.Encoding.UTF8;
// Send SMTP mail
smtpClient.Send(message);
lblSuccess.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblSuccess.Text = "Send Email Failed.";
}
}
i tried to make a simple codes for sending email try this
MailMessage mm = new MailMessage();
mm.From = new MailAddress("fromEmail");
mm.To.Add("toEmail");
mm.CC.Add("ccEmail");
mm.Subject = "StringSubject";
mm.Body = "BodySubject";
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "UsernameString";
NetworkCred.Password = "PasswordString";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
try
{
smtp.Send(mm);
}
catch (Exception)
{
}
Try this reference ASP Simple SMTP for C# and VB it helps me a lot for may smtp problem
Please have a look on google support team, what they are saying regarding sending mail from application.
https://support.google.com/mail/answer/78775?hl=en
Also following link can help you.
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Goto Account of Gmail , then select
Connected apps & sites
Allow less secure apps: ON(if this is off you cannot send mails through apps,or your websites )

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

Not able to send mail from localhost through yahoo,gmail smtp server

What is wrong in below mentioned asp.net code while sending test mail from my localhost web application?
Error: The SMTP server requires a secure connection or the client was
not authenticated. The server response was: 5.7.1 Authentication
required
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "abcdefg#gmail.com";
string password = "12345";
string emailTo = "zyxw#gmail.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
The server requires an authentication. Yahoo doesn't just send emails for anybody. I don't think you can send an email through their gateway using a Google account.

Categories

Resources