Windows Azure 環境自体は、現在 SMTP リレーまたはメール リレー サービスを提供していません。Steve Marx は、次のことを行う優れたサンプル アプリケーション (ここからダウンロードできます)をまとめています。
続く:
サード パーティのサービス (SendGrid) を使用して、Windows Azure 内から電子メールを送信します。
入力エンドポイントを持つ worker ロールを使用して、ポート 25 で SMTP トラフィックをリッスンします。
CDN エンドポイントでカスタム ドメイン名を使用して BLOB をキャッシュします。
受信メールを処理するコードは次のとおりです。
// make a container, with public access to blobs
var id = Guid.NewGuid().ToString().Replace("-", null);
var container = account.CreateCloudBlobClient().GetContainerReference(id);
container.Create();
container.SetPermissions(new BlobContainerPermissions() { PublicAccess=BlobContainerPublicAccessType.Blob });
// parse the message
var msg = new SharpMessage(new MemoryStream(Encoding.ASCII.GetBytes(message.Data)),
SharpDecodeOptions.AllowAttachments | SharpDecodeOptions.AllowHtml | SharpDecodeOptions.DecodeTnef);
// create a permalink-style name for the blob
var permalink = Regex.Replace(Regex.Replace(msg.Subject.ToLower(), @"[^a-z0-9]", "-"), "--+", "-").Trim('-');
if (string.IsNullOrEmpty(permalink))
{
// in case there's no subject
permalink = "message";
}
var bodyBlob = container.GetBlobReference(permalink);
// set the CDN to cache the object for 2 hours
bodyBlob.Properties.CacheControl = "max-age=7200";
// replaces references to attachments with the URL of where we'll put them
msg.SetUrlBase(Utility.GetCdnUrlForUri(bodyBlob.Uri) + "/[Name]");
// save each attachment in a blob, setting the appropriate content type
foreach (SharpAttachment attachment in msg.Attachments)
{
var blob = container.GetBlobReference(permalink + "/" + attachment.Name);
blob.Properties.ContentType = attachment.MimeTopLevelMediaType + "/" + attachment.MimeMediaSubType;
blob.Properties.CacheControl = "max-age=7200";
attachment.Stream.Position = 0;
blob.UploadFromStream(attachment.Stream);
}
// add the footer and save the body to the blob
SaveBody(msg, bodyBlob, message, container, permalink);