C# smtp response - c#

i have few question about smtp
im using this code to send mails if the host is gmail then it act diffrente:
foreach (string host in hosts)
{
SmtpClient sc = null;
try
{
if (emailDomain.ToLower() == "gmail.com")
{
MailSend.MailSendApp.EventLog.WriteEntry("mail to gmail.com");
sc = new SmtpClient("smtp.gmail.com", 587);
sc.UseDefaultCredentials = false;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Credentials = new NetworkCredential("UID#gmail.com", "PWD");
sc.EnableSsl = true;
} }
else
{
sc = new SmtpClient(host);
sc.Send(mailMessage);
break;
}
is it possiable to get answer from smtp :
1. that the email arrived
2. if the mail exists
thanks

If you want to receive a notification that the email has arrived you need to send the email with delivery notification options.
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
If the email doesn't exists, you will get an email back to your sender address and not to your SMTP class.
In short, there's no easy way to determine these two thing purely from the SMTP class perspective.

Back in the day you could directly query whether the email address exists on a given server (VRFY user SMTP command). Then, SPAM was born and pretty much all email servers removed that capability because spammers would use bots to query possible email recipients on each mail server to build SPAM lists.
You will still get an ordinary bounce report (email back to the reply-to address indicating that delivery failed). I use a tool called Boogie Tools to automate processing of bounce messages.
Gmail does not offer delivery or read receipts (though some email servers still optionally allow it) for similar reasons... too much abuse potential.

Related

How to send message to hotmail using asp.net without user password?

I have a web page, In CONTACT US tab I have a forms user can only enter their Name, Email Id, Subject and message. Once they click the ok button I want to get those message to my hotmail account.
I tried some code. But it doesn't work.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUserEmail.text, txtPassword);
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.Subject = "Demo";
msg.Body = "Hi there..";
string toAddress = "xxx#hotmail.com"; // Add Recepient address
msg.To.Add(toAddress);
string fromAddress = "\"no reply \" <from#gmail.com>";
msg.From = new MailAddress(fromAddress);
msg.IsBodyHtml = true;
try
{
smtp.Send(msg);
}
catch
{
throw;
}
This code I tried. But it's having a password. I want user to send email without password to my xxx#hotmail.com
Here is my form design
That depends on your host. Usually web hosts give you a local SMTP server, then you can use it to send any mail you want, just need to know the configuration settings and use them with the SmtpClient.
If your host doesn't offer smtp (very strange unless you're selfhosting the page) you can:
1-Install a local SMTP server (if you manage the server), this is the preferred solution.
2-Use an external service like google to send the mails, but then you need to create an account on the service and use these credentials, and have in account that Google has a lot of restrictions sending emails (limit per second, marking mails as spam, etc etc).
Preferred way to do this is to not have the mail be sent from the users e-mail, but rather have a dummy e-mail that sends the mails and contains the data the user entered. Not the best solution probably, but it doesn't require user credentials.

ASP.NET send mail to multiple users

Hello i have a problem with sendig mails from asp.net web site. Here is my code
private void mailgonder(string mail, string adsoyad)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress("my mail address", "my name");
mm.Subject = "YENİ DUYURU";
mm.Body = "Sistemde okumadığınız yeni bir duyuru bulunmaktadır.";
mm.To.Add(new MailAddress(mail, adsoyad));
SmtpClient sc = new SmtpClient("my smtp client");
sc.Port = 587;
sc.Credentials = new NetworkCredential("my username", "my password");
sc.Send(mm);
}
I want to send mail to 1500 users with same time. How can i do this in asp.net (we will have over 1000 member in this web project)
I can send like this to one person. But i don't know how can i send this to multiple persons
Thanks
As MailMessage.To is a collection, you can add as much as you want :
mm.To.Add(new MailAddress("user#user.com", "user1"));
mm.To.Add(new MailAddress("user1#user.com", "user2"));
mm.To.Add(new MailAddress("user2#user.com", "user3"));
mm.To.Add(new MailAddress("user3#user.com", "user4"));
.....
until
mm.To.Add(new MailAddress("user1499#user.com", "user1499"));
mm.To.Add(new MailAddress("user1500#user.com", "user1500"));
NOTE :
Restrictions may exist according to your mail server or access provider. For more efficiency, use a mailing list.
You have to send each user seperate emails. Do not, never, add more than 1 address to TO unless you have sender score certification, doing opposite will cause you get banned from mail providers since they would think you are a spammer. And also spam list is global, which means if you get banned from gmail, you will also get banned from hotmail or yahoo in a short period of time.
Just select for example 20 email addresses from database each time and loop that 20 and call your mailgonder method for each.
Other than what I wrote above, if you write multiple TO's, each of your users will see each others email adressses which is not cool from user perspective.
I Believe the simplest approach by far is to make a contact group. Doing this will keep the amount of code written to a minimum. Here is a link explaining how to do so in Outlook How to make a contact group
Once you have the contact group setup, simply do this:
String Devemail = "devgroup#whatever.com";
MailMessage message = new MailMessage();
message.To.Add(Devemail);
//The rest of your code here
The rest of your code will work fine.
If someone has a better/simpler approach, please feel free to correct me.
EDIT: I am kind of shocked that we recommend he hard code 1500 email addresses into his code. Can someone give me a good reason for using that approach over using an email group?

When I want to send a email from a website published on Arvixe , where is the smtp server?

After I created a website on Arvixe, we'll call it
www.abcd.us
Accompanying the website, it also creates a email server called:
http://mail.abcd.us/
Because I need send email from this website, I created a email account named : wp#abcd.us
My question is : when I want to send an email from this website using c# code :
SmtpClient client = new SmtpClient("SERVER.arvixe.com", 465)
{
Credentials = new NetworkCredential("wp#abcd.us", "myAccountPassword"),
EnableSsl = true
};
MailAddress from = new MailAddress(#"wp#abcd.us", "wp");
MailAddress to = new MailAddress(#"ToAddress", "ToWho");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
// set subject and encoding
myMail.Subject = "111";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
myMail.Body = "Hi test ";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
myMail.IsBodyHtml = true;
client.Send(myMail);
When run at this statement:
client.Send(myMail);
The code always fail to send. I guess the smtp server may be wrong.
"SERVER.arvixe.com", 465
But I do not know what the correct server is. Where smtp server after I create a website on Arvixe?
Based on what I can see from Arvixe's Knowledge Base, there are two options. If you want to use SSL, you will need to use mail.webeasyserve.com port 465. If you don't want to use SSL, then use mail.abcd.com port 26.
However, this knowledge base article is two years old, so it is possible the information is out of date. Hopefully it will point you in the right direction, even if it is no longer correct.
The domain can also be used as "localhost" and another mail port 25 (non SSL). The mail server at Arvixe can also be accessed by mail.yourdomain.com .

Sending email from web site using Google Apps account

Been researching this issue for quite a while, but have yet to find any solution.
I can send email from the website using my regular gmail account using smtp.gmail.com as my host and port 587.
My current problem is that there's no problem sending the mail. I no longer receive an error. However, the email is never sent. Anyone have any ideas?
Here's the code:
Config:
<smtp from="admin#domain.com">
<network host="smtp.gmail.com" password="password" userName="admin#domain.com" port="587"/>
</smtp>
Code:
public void Send() {
bool bDev = ConfigurationManager.AppSettings["dev"] == "true";
MailMessage oMsg = new MailMessage();
foreach (string sAddress in To) {
if (sAddress != "") oMsg.To.Add(sAddress);
}
oMsg.From = ((FromName == null) || (FromName == "")) ?
new MailAddress(From) :
new MailAddress(From, FromName);
oMsg.Subject = Subject;
oMsg.Body = Body.ToString();
oMsg.IsBodyHtml = true;
oMsg.BodyEncoding = Encoding.UTF8;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = (new int[] { 587, 465 }).Contains(smtp.Port);
smtp.Send(oMsg);
}
I added a second To address and resumed testing. I began to receive email to both accounts. Then after removing the second address, it started working.
I ran into a new issue where my reply-to address was using the From address. So, I added additional code to set the reply address to that of the person's email address from my form.
Code
if (!string.IsNullOrEmpty(ReplyToAddress))
oMsg.ReplyTo = new MailAddress(ReplyToAddress);
However, I ran into an issue with Gmail ignoring my defined reply-to address, and using my from address. Apparently this is by design. I couldn't find any useful information on how to override this setting, so instead I did a workaround that might prove to be helpful to anyone else having this issue.
I created a generic email address with gmail (website#gmail.com) and setup a filter. Any emails coming from admin#domain.com need to be redirected to admin#domain.com.
Now, when I run my tests, all the emails are going where they are supposed to go AND the reply to field works great.
Not the best solution as it involves me remembering to setup a new account every time I run into this issue, but it's a solution that works for the time being until a better alternative comes up.
Had exactly same issue, that no error happened but also no email was sent. Google sometimes blocks your account(usually during testing). Try logging in into gmail account and see what it says.
UPD1: also check your sent folder in gmail.
UPD2: Also make sure that "From" email is the same as your gmail email address.

Sending emails on behalf of user

So im implementing a mailservice in our webapp that users can utilize to send out emails to persons on a certain list. Now i need to know how to best use the System.Net.Mail object to send mail on the users behalf. Ive been trying this now for a while without the right results.
I would like the mail to read "our system on behalf of user 1" and the reply to adress should be the adress that user1 has in our system so that when the contacted person wants to reply to the mail he should get user1:s address, not ours. How can I do this?
Which fields do I need to declare and how? This is how i have it set up right now, but a reply to these settings sends a mail back to noreply#oursystem.com
from = 'noreply#oursystem.com'
replyTo = 'user1#privateaddress.com'
to = 'user1#privateaddress.com'
sender = 'user1#privateaddress.com'
cc = ''
ReplyTo is obsolete. You should use ReplyToList
Example:
MailAddress mailFrom = new MailAddress("noreply#oursystem.com");
MailAddress mailTo = new MailAddress("user1#privateaddress.com");
MailAddress mailReplyTo = new MailAddress("user1#privateaddress.com");
MailMessage message = new MailMessage();
message.From = mailFrom;
message.To.Add(mailTo); //here you could add multiple recepients
message.ReplyToList.Add(mailReplyTo); //here you could add multiple replyTo adresses
This was caused by a programming error on the receiving end. The correct solution is to set the from and replyto as above. That will get the correct behaviour.

Categories

Resources