2016-05-02 24 views
-1

私はフォームを通じてメールを送信するコードを作成しました。 n電子メールを送信していません

protected void SendMail() 
{ 

    string firstName = fName.Text.ToString(); 
    string lastName = lName.Text.ToString(); 
    string event = eventName.Text.ToString(); 
    string phoneNum = phone.Text.ToString(); 
    string pass1 = pass.Text.ToString(); 
    string address1=address.Text.ToString(); 
    string email = gmail.Text.ToString(); 
    string body = "From: " + firstName+" " +lastName+ "\n"; 
    string subject = "title " + event; 
    body += "Email: " + email + "\n"; 
    body += "Event: " + event + "\n"; 
    body += "Phone Number: " + phoneNum + "\n"; 
    body += "Password: " + pass1 + "\n"; 
    body += "Event address: " + address1 + "\n"; 
    // smtp settings 
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); 
    mail.To.Add("[email protected]"); 
    mail.From = new MailAddress("[email protected]", "title", System.Text.Encoding.UTF8); 
    mail.Subject = "title"; 
    mail.SubjectEncoding = System.Text.Encoding.UTF8; 
    mail.Body = body; 
    mail.BodyEncoding = System.Text.Encoding.UTF8; 
    mail.IsBodyHtml = true; 
    mail.Priority = System.Net.Mail.MailPriority.High; 
    SmtpClient client = new SmtpClient(); 
    client.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); 
    client.Port = 587; 
    client.Host = "smtp.gmail.com"; 
    client.EnableSsl = true; 
    try 
    { 
     client.Send(mail); 
     Response.Redirect("sadasd.aspx"); 
    } 
    catch (Exception ex) 
    { 

    } 
} 

私の問題は、電子メールは大混乱であると\nが動作していないということです。これが私の方法です。どうすれば回線をダウンできますか?なぜそれは働いていないのですか?

+2

「\ n」ではなく「Environmen.NewLine」を使用できます。改行文字はオペレーティングシステムに依存します。 –

+1

'\ n'を' + Environment.NewLine();に置き換えてください。 – MethodMan

+2

メッセージの本文はhtmlです。してみてください
Marusyk

答えて

3

私はこの"\n""<br />"に置き換える必要があります。 StringBuilderはMailMessageの構築に適しています。

StringBuilder mailBodyBuilder = new StringBuilder(); 
mailBodyBuilder.Append("From: " + firstName +" " + lastName + "<br />"); 
mailBodyBuilder.Append("Email: " + email + "<br />"); 
mailBodyBuilder.Append("Event: " + event + "<br />"); 
mailBodyBuilder.Append("Phone Number: " + phoneNum + "<br />"); 
mailBodyBuilder.Append("Password: " + pass1 + "<br />"); 
mailBodyBuilder.Append("Event address: " + address1 + "<br />"); 
// rest of contents here 
// send the mail 
関連する問題