send email via asp.net 3.5 - c#

Im looking to send email vi a .net 3.5 form (C#)
Ive seen a few posts elsewhere and got the thing running ok for certain addresses but not for others, eg gmail accounts.
The finished app will have to pick up addresses from a database so Im having to cater for a lot of possibilites.
Im not looking to have my work done for me, just a shove in the right direction would be cool!
thanks
DD

Is it possible the gmail accounts are blocking you as a spammer? If you're sending out a lot of emails, or if there is a configuraiton issue on your server, they may have blacklisted you.
If the emails are going out from your server OK, but not reaching ALL of the recipients but is reaching SOME, then there is likely an issue outside of your direct control. An email goes through several servers in between your server and the enduser's inbox (and that's the case even if the inbox is on google's server for gmail.)
That said, there are things you can do to reduce your likelihood of being blocked/blacklisted.
This is a good place to start: http://www.andreas-kraus.net/blog/tips-for-avoiding-spam-filters-with-systemnetmail/
and then here is where to go from there. (Sorry - it's just a google search, but I gave you the relevant terms, at least.) http://www.google.com/search?q=system.net.mail+blacklist+spammer&rls=com.microsoft:en-us&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1
Finally, here's a very good resource with a cool URL. Perhaps the force will be with you if you learn these ways, young padawan.
http://www.emaildeliveryjedi.com/avoid-spam-filters.php
But getting back to your original question - everything you need to know about how to write code to send an email is covered here: http://www.systemnetmail.com/ It's the same for all versions of .NET from 2.0 on uo through the current 4.0.
And I'm sure you're already aware, but in case you're not, be sure you're familiar with the CAN-SPAM act. http://business.ftc.gov/documents/bus61-can-spam-act-compliance-guide-business

In addition to the great info from David Stratton...If you're certain that the gmail accounts are not receiving your emails, i.e. you've waited for sometime, checked the gmail spam boxes, and you've checked your SMTP server logs to ensure that Google is not sending any particular messages back to your SMTP server, then you may have run into a case of grey-listing / black-listing.
Black-listing can occur when a mail service has decided that the IP address of your mail server is a spamming server. It can happen under different scenarios, shared scenarios can be quite common.
One shared scenario: Let's say you recently acquired a new IP address, but it was recycled by your hosting provider from a prior client of theirs, who was a spammer. There is no guarantee that the IP address would be white-listed.
Another shared scenario: You're sending emails on a shared hosting website, where your SMTP service that you are using is shared with other clients of the SMTP service provider. It may be that one of those clients used the SMTP service maliciously or without following appropriate SPAM rules that gmail abides by.
Your best bet is to contact Google to commence a white-listing process. Providing you follow their requirements, they should eventually unblock emails originating from your service.

Related

What is the best way to handle email distribution across many applications?

I have many applications across an enterprise environment and they all use different methods of sending emails. Some send directly through an exchange server, some queue up locally in an SMTP queue and others call a a web service that then sends the email.
I'm trying to decide on the best way to get guaranteed delivery of emails. If our Exchange server goes down, then the applications that send to it directly can no longer send emails, also any emails sent during the down time never get anywhere. I would also like to implement a universal templating solution that all applications can share.
Are there any pre-built solutions to this problem, or do you have an insight on how to handle this issue?
We solved this by creating a web service that sends all our emails. This web service uses the
System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis
setting, which essentially saves the files to a spot on the disk, tries to send them via the main SMTP server, and if the server is unavailable, they sit in the directory until it BECOMES available.
Guaranteed delivery, as long as the web service is up. Since we have redundancy checks in place, this is almost never an issue. If it is, we treat it as an error in code and handle it.
edit - added
I forgot to mention that XSS is a concern even in an email, so be sure to use something like the Microsoft.Security.AntiXss library, which contains functions like GetSafeHtmlFragment to strip out potentially dangerous scripts before outputting html to an email.
http://msdn.microsoft.com/en-us/security/aa973814.aspx
http://msdn.microsoft.com/en-us/library/aa973813.aspx
I have heard good feedback about Postmark. Maybe a service like that could be solution as it has several integration points.
http://postmarkapp.com
We have used HMailServer (on windows platform) which is freeware. Configured the max retries to too many & used external smtp to relay the emails. our applications ques up emails on the HMailServer and that server relays it further. with the max retries configured to be many if at all the main smtp servers are down we can assure that the email are delivered - but though there is no gurantee if there is huge downtime with main smtp relay servers.
I do this by queuing email to a SQL server database. Any ACID compliant database will work or you can use MongoDB with 'safe mode' inserts but if you really need guaranteed then use SQL server or MySQL. This way if your mail gets into the database it is 'guaranteed' not to be lost and your app doesn't have to think about it. You could use a web service or just make a shared assembly with a static public method in a class to drop your email in the db for you.
Include a column for status like 'new', 'delivered', 'recipient mailbox temporarily full', which you can represent with numeric values and keep a TimeToSend column, which starts out as the time when the email is queued in the database.
Then you have a mail app, that you can have run once a minute as a windows scheduled task. Make it as a console app. When it loads, it checks if an instance of it already running and if there is one, it exits. When running:
1. Attempts to deliver each mail to mail server. Query the database for all mail where the TimeToSend is older than now.
2. If mail is delivered to mail server, mark it logical deleted.
3. If any mail can't be delivered, advance the TimeToSend column for them to 10 minutes from now.
4. Delete records from table that are logically deleted. You can do this in the app or you can do it by having a sql job do it.
As mentioned earlier, you can utilize a web service that you can usually POST JSON to using an HTTP request. Here are a bunch of choices:
PostageApp (Ours!)
SendGrid
PostmarkApp
Amazon SES
They all have different feature sets and offerings, so definitely give them all a spin and figure out which you prefer.
(Full Disclosure: I am the Product Manager of PostageApp.)

Email Anonymization Similar to Craigslist in C#

I am developing a site for which I would like to protect buyers by anonymizing their email addresses.Similar to craigslist's system, when a seller needs to contact a buyer they should be able to send an email to an anonymized address such as 1425415125#mysite.com which will then be routed to the user's email address.
My plan right now is to:
Set up a bucket (catch-all) inbox
Generate a random key for each buyer which will be the user specific ('1425415125' above) section of the email address
Monitor the bucket inbox and parse out this user specific section. Once I know the user, the email can be forwarded to the correct address
My questions are as follows:
Can you see any issues with the above solution
Are there any open source solutions to the existing problem
Are there any gotchas that one should be aware of when developing such a system?
Thanks in advance
JP
I did something related, though not quite the same. I setup a catch all inbox on my existing pop3 server (you probably have one already I'm guessing). I then used OpenPop.NET to read all new messages on a timer (say every 30 seconds). In my case I stopped at just processing the messagse but it's easy enough to generate a new message to the appropriate address and copy the body over, then send the new message out on your SMTP server.
One problem I see with your setup, and maybe it's just a misunderstanding on my part, is that while you are protecting the users original email address they will continue to be reachable at 1425415125#mysite.com basically forever. If I understand the way craigslist works, each posting has a different email address, and once the posting has been deleted/removed (or shortly after) the email address stops working. This makes it so that people can't just keep bugging you on that email address. The solution to this issue is easy, just make the email address coorespond to a post id or some other id rather then the users id in the database. The lookup will be just as quick but they will have a new email address each time.
You may wish to look at mail "piping" - the ability for someone to send an email to a mail server, which then gets thrown immediately to an executable, which then forwards your mail onto the recipient (by pulling the real email address from the database based on the incoming address from the piped message).
My personal recommendation would be to check out HMailServer, which has a COM API (the admin side is written in PHP, hence the requirement for legacy interop), is free and open-source, and is very well-documented. It doesn't have mail piping built-in, but is easily extensible given the API and support for scripts which run on server-side message events
HTH,
Benjamin
I think this solution will make sense and is in use in a lot of cases. The hardest part is actually receiving the messages. You can actually handle all of this within your web app if you need to. I wrote a blog post highlighting a couple of ways to receive email in your web app. It applies mainly to Rails but the concepts should be transferable.
The way you are lookimg to do it is the way I created a similar service. I would not recommend you writing your own smtp server. Use an existing mailserver and just use polling or some event based api.
The benefits of using a 3rd party mailserver is you can use existing backup and management tools on it.
Edit: I just noticed this has beed answered here with a better explanation. Pipe incoming email to a script on Windows IIS SMTP?
I do not see any problem with your setup, infact that is correct way to do because if your scheduled application fails, the emails will still be in the catch-all email box. Only once the email has been successfully delivered to somebody, the email should be deleted. You will be able to monitor and log the activity of your own application to monitor progress and failures.
I do not recommend Piping because, if for any reason piping goes successfully but your exe crashes, you will loose the email. Tracking will be difficult. Scheduling the jobs will not be possible.
If your application is independent of mail server, it is easy to manage it and replace your mail server whenever possible. It is easy to extend.
In this you will have to use some pop reader library, and schedule your application to run frequently.
In addition to email, you may consider a pull rather than push delivery mechanism, e.g: a message center web frontend or RSS feed. I say this because deliverability problems to various ISPs can be very difficult to troubleshoot and in my experience your users will never believe it's their ISP.

ASP.NET Email Patterns

ASP.NET Webforms application.
Problem: How to implement emailling elegantly?
Many parts of the application demand it. This will include HTML emails, solutions how to handle bouncebacks etc..
I have the option to use my own SMTP server (app is hosted on a VPS), or to use the hosting providers.
I'd like to start logging here too (log4net probably)
Any suggestions for libraries to use / patterns to implement?
EDIT: http://martinnormark.com/2010/03/13/generate-html-e-mail-body-in-c-using-templates
I like having a debug catch asp.net net.mail - route all emails to a different email address in debug mode
For testing with a faked out SMTP server use ndumbster. Phil Haack has a blog post on usage - http://haacked.com/archive/2006/05/30/ATestingMailServerForUnitTestingEmailFunctionality.aspx
As you pointed out email templates are fantastic. Consider creating a template for HTML and plain text. Make sure your HTML email templates use basic HTML as a number of email clients out there are not up to modern day browser standards (eg. Outlook which uses the Word HTML rendering engine).
Log4Net is a good point to start at with logging. You can also use System.Diagnostics for simplistic tracing and debugging which can be written easily out to the event log or a file. I always prefer to have a wrapper for all logging so that logging frameworks can easily be swapped out if you find that you want to swap them out later because of a must have feature. It's also good from the point of testability as you can mock it out easily.
Really it doesn't matter which SMTP server you use as long as you don't get black listed. Make sure you check the SMTP servers IP against a DNSBL so you know if your SMTP host has a bad reputation. I highly recommend checking it using Barracuda Central's IP reputation - http://www.barracudacentral.org/lookups. Also make sure you're SMTP server does support resending when you're recipients use grey listing.
For bounce backs you can setup a POP account for it to send back to, it could be as simple as reading the emails and finding which email account the rejection came from and the subject of the previous email (which should be unique) so that you can send again later or after several bounce backs remove them from the list. To implement reading POP take a look at this thread - Reading Email using Pop3 in C#
The best pattern you can implement to save yourself loads of trouble down the road is not to send email directly from your web application. Instead, write the email (or the info you need to generate the email) to a database and have a separate process do the mailing.
Consider what happens when your smtp server is down and your app tries to send. If you have a separate process sending email you can send the email later when its back up.
Also you gain the ability to rate limit your outgoing email. If you end up with the Next Big ThingĀ® on your hands you'll be glad you now have the ability to prioritize your outgoing mail or send mailing lists in batches instead of crushing your smtp server at the high point of your site's traffic.
If you are working with bouncebacks just have this process handle that as well. Now you have the ability to log a returned email address to a blacklist and check against the same list for every outgoing email. No way you want to have to put all that in your web app.
Regardless of what else you come up with please consider just writing the email to be sent to a database and letting your app go about its business. You can still get the message going out in near real time but you can get a real good separation of concerns with little effort.
As for incoming messages, I have had great success with the hMailServer which is a free e-mail server for Microsoft Windows. It supports the common e-mail protocols (IMAP, SMTP and POP3) and can easily be integrated with many existing web mail systems. It has flexible score-based spam protection and can attach to your virus scanner to scan all incoming and outgoing email. Definitely worth looking into.
For outgoing emails, I like the patterns I've expressed in this article: .NET MailMessage, LinkedResources, AlternateViews and Exceptions

Sending Email to a specific address without requiring user to specify their mail server details

Can anyone recommend a simple and reliable method of sending email notifications and possibly log files attachments from a C# program without requiring the installer or the user to configure the program by specifying server details and email addresses etc.
(Mainly because they won't know the details, but also because they could change)
The program will normally be run as a service of a Windows Server, but can be run on a client.
I tried connecting to our own mail server and sending a email to myself, but some ISP's are blocking Port 25 on all servers but their own, so that method isn't working reliably.
Tried sending email through gmail but that was less successful as the port they used was blocked by firewalls. Ditto webservices connecting on weird ports.
Trying to use the local smptservice but did not work either.
It would be nice, but not essential if it was not dependant on my own Internet connection/Servers. (Don't mind them being delayed, but prefer them not to get lost).
Are there any webservices on http/https that allow you to do this sort of thing?
TIA
try using cdosys
http://support.microsoft.com/kb/310212
http://www.eggheadcafe.com/articles/20030316.asp
May have discovered the solution.
Was catching up on my blog reading over the weekend and came across a recent entry on Coding Horror and the very first comment mentions PostMarkApp which seems to do everything I need (and almost everything I want, apart from attachments which they are considering).

Creating an IMAP client application in Silverlight

I'd like to make a stripped down email client for my pre-schooler using Silverlight 3 and pulling email from a Gmail account.
I'll have some filters setup in Gmail so that only a subset of email is given a particular label, similar to creating a whitelist. Then, I'd like to pull those emails with that label to the Silverlight client. I'd like to avoid running any of the messages through the server (so that I can share this application with friends and not have their email app require a server).
I've never written any sort of email client (POP3 or IMAP) and am not sure if this will even be possible. Looking through the various libraries available for retrieving via IMAP, I can't find references to using a browser-limited client such as Silverlight.
Also, I'm guessing I'll be able to send via .NET built in SMTP objects in Silverlight, but haven't tested this yet either.
Can anyone point me in the right direction; tell me why this may or may not be feasible; or relate their own experiences regarding this type of challenge?
Silverlight does not yet allow arbitrary socket connections, which you would need to connect to an IMAP server on the privileged port of 143. Silverlight can only connect to servers, even with a client access policy file, on ports 4502-4534.
Your only options are to proxy to gmail via a server on those ports, or just do the IMAP work on the server and serve it down to the client app over HTTP.
Sorry about this-- enhanced socket support is always being looked at, but it has scary security implications and hasn't been implemented yet. Good luck finding a solution to your scenario.
There is a great example of a Silverlight based mail client here:
http://silvermail.com.au
I use this regularly to check my personal email from work, and I know that it works with GMail.
Hope that helps.

Categories