我試圖在內部網上發送郵件。我設計了一個頁面,其中的值將從文本框接受,並傳遞到相應的字段。SMTP郵件不能正常工作
這是我的前端代碼(請忽略<tr>
和<td>
標籤):
<td>
FROM
</td>
<td>
<asp:TextBox ID="txtFrom" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
</td>
<td>
SENDER MAIL
</td>
<td>
<asp:TextBox ID="txtMailAdd" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
<td>
SMTP IP
</td>
<td>
<asp:TextBox ID="txtSMTP" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
<td>
RECEIVER
</td>
<td>
<asp:TextBox ID="txtReceiver" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
<td>
TO MAIL
</td>
<td>
<asp:TextBox ID="txtTo" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
這是我的後端代碼:
protected void btnSMail_Click(object sender, EventArgs e)
{
string smtpadd = txtSMTP.Text;
try
{
if (smtpadd != "" && smtpadd != null)
{
MailMessage mm = new MailMessage();
SmtpClient sc = new SmtpClient(txtSMTP.Text);
if (!fupAttach.HasFile)
{
FileStream fs = new FileStream("D:\\DEV\\New.xml", FileMode.Open, FileAccess.Read);
Attachment attch = new Attachment(fs, "License Generation in XML", MediaTypeNames.Application.Octet);
mm.Attachments.Add(attch);
}
//sc.Host = "10.8.4.118";
sc.Port = 25;
sc.EnableSsl = false; // runtime encrypt the SMTP communications using SSL
mm.To.Add(new MailAddress(txtTo.Text, txtReceiver.Text));
mm.From = new MailAddress(txtMailAdd.Text, txtFrom.Text);
mm.Subject = txtSub.Text;
//mm.To= new MailAddress(txtTo.Text);
mm.Body = txtBody.Text;
if (fupAttach.HasFile)
{
String FileName = fupAttach.PostedFile.FileName;
Attachment mailAttachment = new Attachment(FileName, MediaTypeNames.Application.Octet);
mm.Attachments.Add(mailAttachment);
}
lblMailFail.Text = "Mail Successfully Sent";
sc.Send(mm);
}
else
{
lblMailFail.Text = "Enter an SMTP IP";
}
}
現在,當我嘗試輸入以下值文本框
Sender Mail: [email protected]
SMTP ID: 10.8.4.2
Receiver Mail: [email protected]
我得到一個錯誤
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Unable to relay for [email protected] at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at Lic_Gen.btnSMail_Click(Object sender, EventArgs e)
有人能指導我,我哪裏會出錯?
確保你使用正確的SMTP端口。 –
SMTP服務器不允許中繼(即將電子郵件轉發到其他服務器)。您必須配置SMTP服務器以允許中繼或切換到另一個。 – jgauffin
雖然作爲一點,不要說它發送之前發送成功。也可以將它放在try catch中,以便郵件服務器可以關閉,如果發生這種情況,那麼這段代碼就會中斷。 –