我是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;
}
我走的是正确的道路吗?
请帮帮我..谢谢
答案 0 :(得分:0)
当您实际发送邮件时,您应该使用try/catch
,您会得到更明确的错误信息:
try
{
client.Send(message);
}
catch (Exception e)
{
//Handle exception
Console.Write(e.ToString()); //Or use another way to display the message.
}
答案 1 :(得分:0)
试试这个,
string mailServer;
int port;
string mailId, mailPass;
string subject;
string mailTo;
subject="something";
StringBuilder mailBody = new StringBuilder();
mailTo = "someone@gmail.com";
mailServer = "smtp.gmail.com";
mailId = "something@gmail.com";
myString.Length = 0;
myString.Append("<html><body><div>BODY CONTENT</div></body></html>");
mailPass = "xxxxxx";
port = 587;
MailMessage mail = new MailMessage(mailId, mailTo, subject, myString.ToString());
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(mailServer, port);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(mailId, mailPass);
smtp.UseDefaultCredentials = false;
smtp.Credentials = nc;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
根据你的意愿改变mailTo,mailId,mailPass。
答案 2 :(得分:0)
我改变了:
this.ErrorPanel.Visible = false;
到
this.ErrorPanel.Visible = true;
看看是否会出错。