receive email through asp.net application - asp.net

I want to receive email to my email address through my asp.net application. Its like someone who send an inquiry through a form. I have used the following code for this and seems like its doing nothing. I remember that I did one of my websites and cant remember how I did it. Please find the code below.
Thanks,
eMessage.To = "info#boilernetworkservices.co.uk"
eMessage.From = txtEmail.Text
eMessage.Subject = "Web Submission"
eMessage.Body = "Web submission received from " & txtName.Text & ". Phone no: " & txtPhone.Text & "."
eMessage.Priority = MailPriority.High
SmtpMail.Send(eMessage)
How can I make this working?

Your sample code shows how to use SMTP to send emails, but you won't be able to retrieve emails from a remote server using this protocol.
The two most common protocols used to retrieve emails are POP3 and IMAP4, and unfortunately the .NET framework doesn't provide an implementation of them like it is done with SMTP.
One option for email retrieval is to use use the open source POP3 client OpenPop.NET, which is discussed in this SO question: retrieve email using c#?.

I set up a google account for my business like myCompanyName#gmail.com.
And I use that as a relay.
You have to set your google account to "Allow less secure apps".
Here is my code to let a potential client fill out a contact us from and send the info to me (Even works when I publish to Azure:)):
private void SendEmailToMyCompany(ContactInfo contactInfo)
{
string message = contactInfo.Message.Replace("\n", "<br />");
MailAddress from = new MailAddress(contactInfo.Email);
MailAddress to = new MailAddress("myhotmailaccount#hotmail.com");
MailMessage mailMessage = new MailMessage(from, to);
StringBuilder body = new StringBuilder();
body.AppendFormat($"<b>First Name:</b> {contactInfo.FirstName}");
body.Append("<br />");
body.AppendFormat($"<b>Last Name:</b> {contactInfo.LastName}");
body.Append("<br />");
body.AppendFormat($"<b>Phone:</b> {contactInfo.Phone}");
body.Append("<br />");
body.AppendFormat($"<b>Email:</b> {contactInfo.Email}");
body.Append("<br />");
body.AppendFormat($"<b>Message:</b><br /><br /> {message}");
mailMessage.Body = body.ToString();
mailMessage.Subject = "MyCompany Customer Contact";
mailMessage.IsBodyHtml = true;
string smtpHost = _config["EmailSettings:SmtpHost"];
string port = _config["EmailSettings:Port"];
string userName = _config["EmailSettings:UserName"];
string password = _config["EmailSettings:Password"];
SmtpClient client = new SmtpClient(smtpHost)
{
Port = int.Parse(port),
Credentials = new NetworkCredential(userName, password),
EnableSsl = true
};
client.Send(mailMessage);
}
And then here is my email settings from app.config:
"EmailSettings": {
"SmtpHost": "smtp.gmail.com",
"Port": 587,
"UserName": "myCompanyNameGmailAccount#gmail.com",
"Password": "**********"
}

Related

Fail Sending Email Unable To Connect the Remote Server

im facing error when sending email. here is the error
what is the actual error for this exception because i newbie in in this, by the way here is my code line:
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("test#gmail.com", "passwordexample")
SmtpServer.EnableSsl = True
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("test#gmail.com")
mail.To.Add("examplemail#gmail.com")
mail.Subject = "Change Request Submitted"
mail.Body = "Dear User, " & Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine & "One Notification have been submitted," & Environment.NewLine & "Please check the change request on the intranet application"
//ad this line the error SmtpServer.Send(mail)
SmtpServer.EnableSsl = True
MsgBox("Notification emailed successfully")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Please see this answer...
Sending Email from Visual Basic
If you still can't send the email....
Have you enabled POP or IMAP in Gmail?
Sign in to the Gmail web interface.Open the 'Forwarding and POP/IMAP' tab on your 'Settings' page, and configure IMAP or POP. After enabling this in Gmail, make sure you click 'Save Changes' so Gmail can communicate with your mail client.
If you have 2 step verification..
Get an Application-Specific Password
Google will automatically generate a password that you will need only once, when you set up Google Mail on your mobile device or email software. You can easily generate a password for each device or email software you want to use.
Step 1:
At the bottom of the 2-step verification screen, next to Application-specific passwords, click Manage application-specific passwords. The Authorized Access to your Google Account screen will open.
Step 2:
On the Authorized Access to your Google Account screen, in the Name field, type in a name to help you remember what application you are using to access your account and click Generate password. You will then see a password which you will use to configure your mobile device or email software. Keep this screen open until you are ready to enter the password (see part 4, below). The password creation with the device name will show at the bottom. When you are done using the password, click Done.
Step 3:
To set up a password for another device or email software, simply type in the name of it in the Name field and click Generate password. You will receive another password.
See more here...
http://www.oit.umass.edu/support/google-apps/configure-google-mail-email-software-mobile-devices
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("EMAIL FROM#gmail.com", "YOUR PASSWORD")
SmtpServer.Port = 25
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Dim omail As New MailMessage()
omail.From = New MailAddress("FROM EMAIL #gmail.com", "Message", System.Text.Encoding.UTF8)
omail.Subject = "test subject"
omail.To.Add("test#gmail.com")
SmtpServer.SendAsync(omail, Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
if that didn't work try
SmtpServer.Port = 587

how to add message in contact us page to inbox?

In every website there is "contact us"
which contains "name ,message,mobno,mailid are may be mandatory and submit(button) "
how to do this in html?
When I click submit buton,is it possible to get message added to gmail inbox automatically????
You can do this. Here is the example
<form method="post" action="mailto:email#serviceprovide.com" >
<input type="submit" value="Send Email" />
</form>
There are many way in which you can send email, you can do it in C#,javascript,any of the email form generator.
You can decide which one to use based on what you actually need and is best satisfied by which method.
You need to Code for send Mail to your account in the submit button event like below
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;// only for gmail account
smtp.Credentials=new System.Net.NetworkCredential("AdminEmail#admin.com","AdminPassword");
smtp.EnableSsl = true;
smtp.Send(Msg);
Also you need a Gmail account (user name and password) to send mail
If you are using any other smtp server then smtp.Port = 25; the port number will change
refer this link

Email confirmation asp.net

This my working method to send a email
Sub emailConfirm()
Dim email As String = txtMail.Text
Dim name As String = txtPrenom.Text + txtNom.Text
Dim mail As MailMessage = New MailMessage()
'mail.To.Add("francois.smtp#gmail.com")
mail.To.Add(email)
mail.From = New MailAddress("sumerman#gmail.com.com")
mail.Subject = "Email using Gmail"
Dim Body As String = "Hi " + name + ", this mail is to confirm your registration" + "Click on the link to confirm please" + " Link(check note) "
mail.Body = Body
mail.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.Port = 587
smtp.Credentials = New System.Net.NetworkCredential("sumerman#gmail.com", "12345")
smtp.EnableSsl = True
smtp.Send(mail)
End Sub
note:I want to send Link contains a unique identifier and verify the unique identifier in the login.aspx
I don't know when to do this...
I found nothing interesting on the net ...
This is in Vb.net
I will add security later
Thank's for your helping!
Well, first off don't put the username and password in the email itself. That's a bit sloppy.
Second, most sites now do the following:
User creates account
Email sent to user. Link in email contains a unique identifier, not the username and password.
User clicks on link. At this point the page tests to see if the unique id is valid. If so, it tells them Thank You for confirming your account. It then displays the login form.
User enters their login credentials.
Note that the email did NOT send the actual username/pw info.

E-Mails sent from online form are coming in one single mail

I'm using an online form at one of my web sites. Every mail sent from this form is coming in one mail even if the senders IP is different.
But I want every single mail to be unique even if the content is same. What I need to do to the mails or which header I need to modify?
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("no-reply#toprakbasim.com", "NoReply");
MailAddress toAddress = new MailAddress("info#toprakbasim.com", "Info");
MailAddress toSender = new MailAddress(tEMail.Text, tNameSurname.Text);
message.From = fromAddress;
message.Bcc.Add(toAddress);
message.ReplyTo = toSender;
message.Subject = tNameSurname.Text + " : contact";
message.IsBodyHtml = true;
message.Body = "some html here";
smtpClient.Send(message);
Gmail will group emails with the same subject line together. Put some text in the subject line to make it unique, like MessageID, time, whatever.
If you are saying the content in the body contains more than one response, then the issue is how you are collecting the text that then gets assigned to message.Body. If the text is in a variable before assigning to message.Body, make sure that you are not re-using the variable and that it gets re-instantiated each time.
What are you using for a mail reader program? Because it sounds like that program is rolling your emails up for you. (Outlook 2010 does this, by default).
Try reading your email with a different email reader (like Outlook Express, or tbird)

Sending Email via IIS SMTP to external address

When using the following code to send an email message to an external e-mail address via IIS6 SMTP I am receiving a message stating that the message has been sent, but it never arrives at the destination. I'm using the System.Net.Mail namespace and the following code:
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
foreach (string strTo in to.Split(';'))
{
if (strTo.Replace(";", "") != string.Empty)
msgMailSummary.To.Add(new MailAddress(strTo.Replace(";", "")));
}
msg.Subject = subject;
msg.Body = body;
SmtpClient sm = new SmtpClient();
sm.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sm.Credentials = new NetworkCredential(tbUsername.Text, tbPassword.Text);
sm.Host = host;
sm.Port = port;
sm.Send(msg);
I don't have a SmartHost setup in IIS6, is there any obvious or any hints,tips that I can check out to get this working?
Umm, you seem to be missing one key line...
msg.To = new MailAddress(to);
I am not sure if I remember right, but I once had a problem where I couldn't send an email because my From address was not what my hosting allowed. Basically I ended up being able to only set ReplyTo and leaving From undefined (the smtp server will define it by itself). Try it, it might work.
Turned out it was the setup on the server wasn't configured correctly.

Resources