我正在使用C#asp.net来获取从查询字符串传递的值,并使用它们发送带有手机号码的电子邮件,以便电子邮件将文本消息发送到该手机。防爆。 9772565555@vtext.com。电子邮件消息作为文本消息很好地到达,但在电子邮件正文中它会切断消息末尾的URL。我确定我正在制作某种语法错误。这是代码
string phone = Request["phone"].ToString();
string item = Request["item"].ToString();
if (phone != null && phone != "")
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add(phone + "@vtext.com");
mailMessage.From = new MailAddress("no-reply@mydomain.com");
mailMessage.Subject = "Your Item"
mailMessage.Body = "Hello. Click the link http://mydomain/order.aspx?order=" + phone + item;
SmtpClient smtpClient = new SmtpClient("localhost", 25);
smtpClient.Send(mailMessage);
Response.Write("<div style='font-size:36px'>E-mail sent!</div>");
}
catch (Exception ex)
{
Response.Write("<div style='font-size:36px'>Could not send the e-mail - error: " + ex.Message + "</div>");
}
}
答案 0 :(得分:0)
为了让其他人更容易通过这个帖子,根据上面的ragerory和Icemanmind的建议,这里是答案。
检查最终邮件长度是否超过允许的字符数限制。在这种情况下,大约120个字符。有很多选择可以解决这个问题,但我会提到两个可能会让你先行一步的选择。
(1)停止发送消息(脏方法)
Const int maxLength = 120;
string messageText = "1234";
Boolean sendMessage = SendMessage(messageText, maxLength);
if (!sendMessage) MessageBox.Show("Could not send message. Character limit exceeded","Error");
Boolean SendMessage(string messageText, int messageCharLimit);
{
if (messageText.Length() > messageCharLimit)
return false;
//TO DO - SEND MESSAGE
return true;
}
(2)将消息分成多个消息并作为多个消息发送(从Split String into smaller Strings by length variable上的SLaks给出的答案中提取)
public static IEnumerable<string> SplitByLength(this string str, int maxLength) {
for (int index = 0; index < str.Length; index += maxLength) {
yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
}
}