2009-09-15 10 views
1

mail.i를 보내는 코드를 구현하려는 iam이 다음 절차를 시도했습니다.ASP.NET 2.0에서 전자 메일 보내기

구성

<table border="0"> 
    <tr> 
     <td><b>Your Email:</b></td> 
     <td><asp:TextBox runat="server" ID="UsersEmail" Columns="30"></asp:TextBox></td> 
    </tr> 
    <tr> 
     <td><b>Subject:</b></td> 
     <td><asp:TextBox runat="server" ID="Subject" Columns="30"></asp:TextBox></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <b>Body:</b><br /> 
      <asp:TextBox runat="server" ID="Body" TextMode="MultiLine" Columns="55" Rows="10"></asp:TextBox> 
     </td> 
    </tr> 
    <tr> 
     <td colspan="2" align="center"> 
      <asp:Button runat="server" ID="SendEmail" Text="Send Feedback" /> 
     </td> 
    </tr> 
</table> 

코드 숨김

protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click 
    '!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS' 
    Const ToAddress As String = "[email protected]" 

    '(1) Create the MailMessage instance' 
    Dim mm As New MailMessage(UsersEmail.Text, ToAddress) 

    '(2) Assign the MailMessage's properties' 
    mm.Subject = Subject.Text 
    mm.Body = Body.Text 
    mm.IsBodyHtml = False 

    '(3) Create the SmtpClient object' 
    Dim smtp As New SmtpClient 

    '(4) Send the MailMessage (will use the Web.config settings)' 
    smtp.Send(mm) 
End Sub 

있지만 작동하지

<configuration> 
    <!-- Add the email settings to the <system.net> element --> 
    <system.net> 
    <mailSettings> 
     <smtp> 
     <network 
      host="localhost" 
      port="25" 
      userName="?" 
      password="?" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 

HTML. 오류는 입니다. 전송 서버에 연결하지 못했습니다.

+0

사용자가 메일 메시지를 만들 때 다음 템플릿을 따르는 것처럼 보입니다. http : //aspnet.4guysfromrolla.co.kr/articles/072606-1.aspx. 단지 도움이되면 누군가가 답을 찾는데 도움이됩니다. – Brisbe42

답변

0

web.config의 MailSettings 란 무엇입니까? 귀하의 주요 문제는 아마 당신이 그것을 잘못된 호스트 또는 포트주고있어 말하고 싶지만.

5

당신은 당신의 코드 줄에 기본입니다 (매개 변수없는) 생성자를 사용하여려면 SmtpClient 개체를 인스턴스화됩니다

'(3) Create the SmtpClient object' 
Dim smtp As New SmtpClient 

이메일을 보내는 유효한 SMTP 서버에 대한 액세스 권한을 가지고 있어야하기 때문에,이 생성자가 시도합니다 web.config 파일, 특히 System.Net 섹션에 정의 된대로 SMTP 서버 설정을 사용하여 개체를 인스턴스화합니다. 아래 예는 다음과 같습니다.

<system.net> 
    <mailSettings> 
    <smtp> 
     <network host="[your smtp server address]" port="[your smtp port - usually 25]"/> 
    </smtp> 
    </mailSettings> 
</system.net> 

이 메시지가 누락되면 SmtpClient 개체에 연결할 SMTP 서버가 없습니다. 이로 인해 발생할 수있는 오류 메시지가 발생할 수 있습니다.

이 문제를 해결하려면이 섹션을 SmtpClient 개체가 연결할 올바른 SMTP 서버를 지정하여 web.config 파일에 추가하거나 인스턴스화 할 때 서버 주소를 직접 생략하고 하드 코드 할 수 있습니다 SMTP 서버 주소/포트 번호를 매개 변수로 받아들이는 오버로드 된 생성자 중 하나를 사용하여 SmtpClient 객체를 만듭니다. 생성자 세부 사항은 here을 참조하십시오.

이의 예는 다음과 같습니다

'(3) Create the SmtpClient object' 
Dim smtp As New SmtpClient("[your SMTP server address]", 25) 

당신이려면 SmtpClient의 생성자에서 SMTP 서버 주소/포트를 지정할 수 있지만, 그것은 일반적으로 이러한 설정을 구성하는 더 좋은 연습 간주 있다는 점에 유의하십시오 web.config 파일을 열고 코드에서 기본 (매개 변수없는) 생성자를 사용하십시오. web.config 메서드를 사용하면 코드를 다시 컴파일하지 않고 사용할 SMTP 서버 주소/포트를 업데이트 할 수 있습니다.

+0

+1 서버 이름을 하드 코딩하지 않는 것이 좋습니다. –

+0

@Stefan - 전적으로 동의하며 제 답변에서 언급하려고했습니다. SMTP 서버 주소/포트를 구성하는 web.config 메서드를 사용하는 것이 더 나은 방법이라고 생각하여 답변을 업데이트했습니다. – CraigTP

+0

@Russ - 편집 해 주셔서 감사합니다. 이제 나는 다음 줄로 넘어서 출혈을 주장하는 VB 코드 주석을 닫는 방법을 알고 있습니다! :) – CraigTP

0

gmail 계정을 사용하여 보내려는 경우. 귀하의 아이디와 비밀번호를 변경하십시오. 다음과 같은 설정을해야 당신의 Web.config :

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
     <network defaultCredentials="false" host="smtp.gmail.com" port="587" userName="[email protected]" password="xxxxxxxxxxxx" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 

귀하의 SendMailFunction은 다음과 같아야합니다

protected void SendMail(String strFrom, String strSubject, String strTo, String strBody) 
{ 
    MailMessage mm = new MailMessage(); 


    mm.From = strFrom; 

    mm.Subject = strSubject ; 
    mm.To.Add(strTo); 

    mm.Body = strBody; 
    SmtpClient smtp = new SmtpClient(); 
    smtp.EnableSsl = true; 

    smtp.Send(mm); 

} 

편집 :는 SO의 Web.config에서 SMTP 설정을 암호화에 대한 자세한 내용 답변 this을 확인하시기 바랍니다