因此,我的计划的主要目的是允许用户在我的网站上填写申请表。一旦填写完网页表单并且用户按下提交,我就已经完成了,所以程序会将他们填写的信息发送到我的电子邮箱。
我遇到的两个主要问题是,除非用户附加网页表单不会发送的附件。这很糟糕,因为他们不需要这样做。所以我需要某种if语句,说该程序仍然应该发送带或不带附件。我也想在发送给自己的电子邮件中发送日期。如何将日期发送到我的电子邮件,用户点击提交按钮。
我已添加以下大部分代码。
谢谢
private string SendMessage(string strTo, string strFrom, string strSubject, string strMessage, string strAttachment, string strBCC)
{
try
{
System.Net.Mail.MailMessage mailMsg;
string strEmail = "";
string strSmtpClient = ConfigurationManager.AppSettings["SmtpClient"];
string[] arrEmailAddress = strTo.Split(';');
for (int intCtr = 0; intCtr < arrEmailAddress.Length; intCtr++)
{
strEmail = "";
if (arrEmailAddress[intCtr].ToString().Trim() != "")
{
strEmail = arrEmailAddress[intCtr].ToString().Trim();
mailMsg = new MailMessage(strFrom, strEmail, strSubject, strMessage);
mailMsg.IsBodyHtml = true;
if (!strBCC.Trim().Equals(string.Empty))
mailMsg.Bcc.Add(strBCC);
/*** Added mail attachment handling ***/
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(strAttachment);
mailMsg.Attachments.Add(attachment);
SmtpClient smtpClient = new SmtpClient(strSmtpClient);
smtpClient.UseDefaultCredentials = true;
smtpClient.Port = 25;
smtpClient.Send(mailMsg);
mailMsg.Dispose();
}
}
return "Message sent to " + strTo + " at " + DateTime.Now.ToString() + ".";
}
catch (Exception objEx)
{
return objEx.Message.ToString();
}
}
protected void Submit_Click1(object sender, EventArgs e)
{
try
{
/*** Moved from SendMessage function ****/
string strUpLoadDateTime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
string strFileName1 = string.Empty;
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
string strUploadFileName1 = File1.PostedFile.FileName;
strFileName1 = strUpLoadDateTime + "." + Path.GetFileNameWithoutExtension(strUploadFileName1) + Path.GetExtension(strUploadFileName1);
strFileName1 = strFileName1.Replace("'", "");
string strSaveLocation = Server.MapPath("") + "\\" + strFileName1;
File1.PostedFile.SaveAs(strSaveLocation);
txtComments.Text = "The file has been uploaded";
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<table>");
sb.AppendFormat("<tr><td>Request Name:</td><td>{0}</td></tr>", txtBugName.Text.Trim());
sb.AppendFormat("<tr><td>Category:</td><td>{0}</td></tr>", ddlModule.SelectedValue);
sb.AppendFormat("<tr><td>Sub-Category:</td><td>{0}</td></tr>", ddlPage.SelectedValue);
sb.AppendFormat("<tr><td>Description:</td><td>{0}</td></tr>", txtComments.Text.Trim());
sb.AppendFormat("<tr><td>Email is:</td><td>{0}</td></tr>", txtemail.Text.Trim());
sb.Append("<table>");
SendMessage(ConfigurationManager.AppSettings["EmailAddrTo"],
ConfigurationManager.AppSettings["EmailAddrFrom"],
txtBugName.Text.Trim(),
strMessage, strSaveLocation, "");
}
catch
{
}
}
答案 0 :(得分:2)
稍微清理一下方法
/// <summary>
/// Sends the message.
/// </summary>
/// <param name="strTo">The STR to.</param>
/// <param name="strFrom">The STR from.</param>
/// <param name="strSubject">The STR subject.</param>
/// <param name="strMessage">The STR message.</param>
/// <param name="strAttachment">The STR attachment.</param>
/// <param name="strBCC">The STR BCC.</param>
/// <returns></returns>
private string SendMessage(string strTo, string strFrom, string strSubject, string strMessage, string strAttachment, string strBCC)
{
try
{
string strEmail = string.Empty;
string strSmtpClient = ConfigurationManager.AppSettings["SmtpClient"];
string[] arrEmailAddress = strTo.Split(';');
foreach (string emailAddress in arrEmailAddress)
{
if (!string.IsNullOrEmpty(emailAddress.Trim()))
{
using (System.Net.Mail.MailMessage mailMsg = new MailMessage(strFrom, strEmail, strSubject, strMessage))
{
mailMsg.IsBodyHtml = true;
if (!string.IsNullOrEmpty(strBCC))
mailMsg.Bcc.Add(strBCC);
if (!string.IsNullOrEmpty(strAttachment))
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(strAttachment);
mailMsg.Attachments.Add(attachment);
}
using (System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(strSmtpClient))
{
smtpClient.UseDefaultCredentials = true;
smtpClient.Port = 25;
smtpClient.Send(mailMsg);
}
}
}
}
return string.Format("Message sent to {0} at {1}.", strTo, DateTime.Now);
}
catch (Exception ex)
{
return ex.Message;
}
}
答案 1 :(得分:1)
了解用户点击的时间:
在您的Submit_CLick1
中添加一行代码来创建邮件:sb.AppendFormat("<tr><td>User clicked submit on:</td><td>{0}</td></tr>", strUpLoadDateTime);
为防止发送电子邮件失败,请使附件成为条件:
if (!string.IsNullOrWhiteSpace(strAttachment))
{
/*** Added mail attachment handling ***/
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(strAttachment);
mailMsg.Attachments.Add(attachment);
}