ASP.net Sending mail using gmail

In order to send mail using a gmail address this can be used:

In Web.config

	<system.net>
		<mailSettings>
			<smtp from="myemail@gmail.com" deliveryMethod="Network">
				<network
					defaultCredentials="true"
					host="smtp.gmail.com"
					port="587"
					enableSsl="true" />
			</smtp>
		</mailSettings>
	</system.net>

And then in the code (in this case C# is used)

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true; //reads the settings from web.config
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message
smtpClient.Send(mailMessage);

Or if you don’t wish to save any settings in web.config

string host = "smtp.gmail.com";
int port = 587;
SmtpClient smtpClient = new SmtpClient(host, port);
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message contained in mailMessage
smtpClient.Send(mailMessage);

SmtClient at msdn
It might also be interesting to read up on mailmessage

PHP Send a mail as UTF-8

To send a mail from PHP encoded as UTF-8 is fairly easy to do by modifying the headers.

//Sender with special chars (UTF-8) encoded
mail('mail@example.com', '=?UTF-8?B?'.base64_encode($subjectString).'?=', $messageString, "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n");
//Sender not UTF-8 encoded
mail('mail@example.com', 'sender@mail.com', $messageString, "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\n");

.forward to several recipients

On *nix environments it is possible to forward the mail to one account to another user (or another email address if external mail is supported).
This is done by adding the recipient in a file called .forward in the home directory.

Example To simply forward to another address:

user@server$ echo recipient@mail.com > ~/.forward
user@server$ chmod go-w ~/.forward

In order to forward to several addresses coma (,) is used as a delimiter between the different recipients.

Example To forward to two addresses at the same time:

user@server$ echo recipient1@mail.com,recipient2@mail.com > ~/.forward
user@server$ chmod go-w ~/.forward

Linux send a log (or output from a command) as a mail from the linux console

Sometimes it is nice to send a log or some other file as a email to someone (or a part of a file)

cat myfile.txt | mail -s 'SUBJECT' example@mail.com;

Change myfile.txt, SUBJECT and example@mail.com to something useful.

It is also possible to send the output of a command to someone

ps -ef|mail -s 'SUBJET' example@mail.com;

exim4 deleting a mail from the qeue

To delete one message from the exim queue

exim -Mrm message-id

To find the ids of all qued mails

mailq

If you wish to delete all the messages to a specific email recipient.

exiqgrep -i -r '<mail@example.com>' | xargs exim -Mrm

Of if you wish to delete all messages that were sent from a specific email address.

exiqgrep -i -f '<mail@example.com>' | xargs exim -Mrm

Linux: Turning off window scaling

A fairly common issue on the Internet is that some routers can’t handle window scaling.
This will might give some odd messages to email servers (such as not being able to send mails over a certain size to one receiving mail server)

On option is to turn off window scaling by editing sysctl.conf and adding the following lines

# Uncomment to turn off window scaling (most usefull for linux MTA)
net.ipv4.tcp_window_scaling = 0

A warning about doing this; In the long run throughput will not be optimal on this server, but sending mails should work better (so best used on a dedicated mail server).

Send mail to several to-addresses

To send email to several recipients use

name <name@example.com>, name2 <name2@example.com>

the part inside the brackes (< & >) is the adress, the part before the < is the name shown as the recipient. The separation char for several addresses is “,”

DSN 5.0.0 – Service Unavaliable

Ran into this error today on a server. Some mails got through but some was not getting through.

Check that /etc/hosts has a FQDN (fully qualified domain name).

Also check the size of the mails being sent, in my case the issue was that mail was to large for the receiving server thus this error message was produced at the sender.

unqualified host name ([something]) unknow; sleeping for retry

sendmail expects to the machine to have a FQDN (fully qualified domain name).

If it does not have that (type “hostname” to check what name the machine has) sendmail will complain about this and take a loot of extra time trying to find it.

To use a FQDN edit “/etc/hosts” and change [something] to [something.yourdomain.com] and this will go away. Updating “/etc/hostname” at the same time might be a good idea; don’t forget to run “/bin/hostname -F /etc/hostname” after updating /etc/hostname.