【发布时间】:2014-06-02 13:10:20
【问题描述】:
我是 asp.net 的新手 .plz 帮助
我正在尝试使用此代码在 C# 中发送电子邮件
任何人都可以告诉我我的代码有什么问题吗??..它正在构建时没有错误但没有发送消息。
if ((!string.IsNullOrEmpty(base.Request["SECURETOKENID"]) && !string.IsNullOrEmpty(base.Request["EMAIL"])) && (base.Request["RESPMSG"] == "Approved"))
{
SqlConnection connection = new SqlConnection(Common.GetConnectionString("DBConnect"));
connection.Open();
SqlCommand command = new SqlCommand(string.Concat(new object[] { "UPDATE VIPPurchases SET PurchaseDate='", DateTime.Now, "', Status=1 WHERE PurchaseCode='", base.Request["SECURETOKENID"], "' AND Status=0" }), connection);
if (command.ExecuteNonQuery() == 1)
{
command = new SqlCommand("SELECT Ct.title, VI.VideoTitle FROM VIPView as VI INNER JOIN VIPPurchases as VP ON VI.catid = VP.VideoCode INNER JOIN categoryppv as Ct ON VP.VideoCode = Ct.catid WHERE VP.PurchaseCode='" + base.Request["SECURETOKENID"] + "' and VI.viewtype='3' AND VI.IsPublished='1'", connection);
string str = (string)command.ExecuteScalar();
command = new SqlCommand("SELECT Value FROM Settings WHERE Name='Email'", connection);
string addresses = (string)command.ExecuteScalar();
command = new SqlCommand("SELECT Fm.FilmmakerEmail, VI.VideoCode, VP.PurchaseCode FROM VIPPurchases as VP INNER JOIN VIPView as VI ON VP.VideoCode = VI.catid INNER JOIN Filmmakers as Fm ON VI.FilmmakerCode = Fm.FilmmakerCode where VP.PurchaseCode='" + base.Request["SECURETOKENID"] + "' and VI.viewtype='3' AND VI.IsPublished='1'", connection);
addresses = addresses + ", " + ((string)command.ExecuteScalar());
command = new SqlCommand("Select Value from Settings WHERE Name='SenderAddress'", connection);
string userName = (string)command.ExecuteScalar();
command = new SqlCommand("Select Value from Settings WHERE Name='SenderPassword'", connection);
string password = (string)command.ExecuteScalar();
command = new SqlCommand("Select Value from Settings WHERE Name='SMTP'", connection);
string str5 = (string)command.ExecuteScalar();
command = new SqlCommand("Select Value from Settings WHERE Name='Port'", connection);
int num = Convert.ToInt32((string)command.ExecuteScalar());
command = new SqlCommand("Select Value from Settings WHERE Name='SSL'", connection);
bool flag = command.ExecuteScalar() == "True";
SmtpClient client = new SmtpClient
{
UseDefaultCredentials = false,
Host = str5,
Port = Convert.ToInt32(num),
Credentials = new NetworkCredential(userName, password),
EnableSsl = flag
};
MailMessage message = new MailMessage
{
Subject = "platformathletics World - Inndividual Registration",
From = new MailAddress(userName)
};
message.To.Add(base.Request["EMAIL"]);
message.IsBodyHtml = true;
message.Body = "<h3>platformathletics World - (" + str + ") Purchase</h3><p>You are allowed to watch training video <b>" + str + "</b> for next 30 days (till " + DateTime.Now.AddDays(30.0).ToString("MMM dd, yyyy hh:mm tt") + "<sup style='color:red'>*</sup>) from now.</p><p>Your ticket number is <b>" + base.Request["SECURETOKENID"] + "</b>.</p><p>Put your ticket number <a href='http://" + HttpContext.Current.Request.Url.Host + "/VIPMovieList.aspx'>here</a> in return box to watch the video.</p><p><a href='http://" + HttpContext.Current.Request.Url.Host + "/login.aspx'>Login</a> or <a href='http:" + HttpContext.Current.Request.Url.Host + "/register.aspx'>register</a> to platformathletics Network to manage your My Pay Per View Purchases section.</p><br /><hr><br /><small>You could be in different time zone other than United States but your ticket will expire in exact 30 days from the time you receive this email.</small>";
client.Send(message);
message = new MailMessage
{
Subject = "platformathletics World - Inndividual Registration",
From = new MailAddress(userName)
};
message.To.Add(addresses);
message.IsBodyHtml = true;
message.Body = "<h3>platformathletics World - (" + str + ") Purchase</h3><p>Your video has been purchased. <i>" + base.Request["EMAIL"] + "</i> is allowed to watch <b>" + str + "</b> for 30 days.</p><p>Confirmation emails are also sent to " + base.Request["EMAIL"] + " and platformathletics Admin.";
client.Send(message);
this.ErrorPanel.Visible = false;
this.SuccessPanel.Visible = true;
}
我是否走在正确的道路上??
请帮帮我..谢谢
【问题讨论】:
-
你试过用 try/catch 包装你的代码吗?
-
是否有运行时异常?
-
未处理的异常会导致程序崩溃。你的程序崩溃了吗?我还建议将您的 Send() 调用包装在 try/catch 中。
-
电子邮件发送可能会出现很多问题。也许您的邮件已发送,但由于垃圾邮件过滤器或接收邮件的邮件服务器上的任何类型的过滤器,您没有收到它。也许“收件人”地址是错误的?等等...这当然是在您的代码在运行时没有崩溃的情况下! (编译只是语法检查,有很多错误编译不会得到)作为旁注,我建议不要进行这么多数据库调用来获取一些记录......也许按类别对您的设置进行分组?
-
为什么我的问题得到-2???