2011-12-14 25 views
3

コントローラからの電子メールの送信方法は?私はデータベースに電子メールアドレスを持っているので、私はベースからこのメールを受け取り、このメールに特別なテキストメッセージを送る。私はビューを使用する必要はありません。コントローラからの電子メールの送信

答えて

9

私達は行く:

あなたのコード:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 

message.From = new System.Net.Mail.MailAddress("[email protected]"); 
message.To.Add(new System.Net.Mail.MailAddress("[email protected]")); 

message.IsBodyHtml = true; 
message.BodyEncoding = Encoding.UTF8; 
message.Subject = "subject"; 
message.Body = "hello receiver"; 

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); 
client.Send(message); 

そして、あなたのweb.configファイル: 1.私はエンコードをhaventは:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.net> 
    <mailSettings> 
     <smtp from="[email protected]" deliveryMethod="Network"> 
     <network host="smtp.yourprovider.com" port="587" userName="[email protected]" password="yourpass" enableSsl="true" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 
... 
+0

私はこの二つの問題を持っています。 2.私はエラー: 'SMTPホストが指定されていませんでした。 ' 私はlocalhostで作業しています:" smpt.gmail.com "と" localhost:5947 " – user1031034

4

MvcMailerを使用できます。または、手動で実装する場合は、SmtpClientクラスを使用して電子メールを送信できます。ここで

関連する問題