Web 开发问题汇总(三)

1.How to send email in java?

A:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {

   public static void main(String [] args) {
      // Recipient's email ID needs to be mentioned.
      String to = "abcd@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "web@gmail.com";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try {
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      } catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

Reference:Java - Sending Email
Send email using java

继续阅读

记一次换新 iPhone

说来惭愧,一直还是在用公司配的开发机 5S, 没用上最新手机的主要原因可以归结为穷 ><。但最近 5S 的存储不够用了,再加上心仪的 8 降价了,一冲动就入了一个,嗯,这几个月做好了吃土的准备了。

iPhone 可以将旧手机数据迁移到新手机,这是我喜欢它的一个地方。苹果这篇将内容从旧 iOS 设备传输到新 iPhone、iPad 或 iPod touch详细介绍几种可行方法,我们可以从中选择一种方法。其实我觉得苹果应该出篇换机指南的,上面这篇文章的题目不是很容易让人把它与换机联系起来,也不是很容易记,这是我想记这么一笔的原因之一,N 年之后我再换 iPhone 的时候也可以按这篇文章的思想来指导自己迁移数据。

这里我选择使用 iTunes 备份恢复,快速开始功能好像不是很灵光,也可能是我不会用,而 iCloud 备份是通过网络,网速会影响备份和恢复,加上最近 iPhone 的 bug 贼多,这让我不是很放心。在此之前有使用过 iTunes 备份恢复,想着即使恢复过程出了问题备份还在,所以最终选择 iTunes。

备份过程很顺利,恢复过程就曲折了,iTunes 提示 backup is corrupt or incompatible,墨菲定理生效了,另一方面也佐证了选择 iTunes 是正确的选择,苹果什么时候能把这软件质量抓一抓,别只顾着赚钱啊。搜索找到了苹果对这个问题的解决办法:

继续阅读

多线程的 Core Data

平常在项目中没有使用过 Core Data, 因为我觉得它的学习曲线还挺陡峭,整个框架给人的感觉很复杂和笨重,因此一直没有使用它。但是看到喵神这份上级向的十个 iOS 开发面试题中和这份百度面试题中都有涉及到 Core Data 的内容,我想还是有必要好好研究一下它,毕竟它是 Apple 官方的持久化方案,我们可以取其精华,弃其糟粕,另一方面未来我们也可能因为各种原因接手或参与使用 Core Data 的项目。

这篇文章主要想探讨上面提到的面试题中的两个关于 Core Data 的问题:

  1. 你实现过多线程的Core Data么?NSPersistentStoreCoordinator,NSManagedObjectContext和NSManagedObject中的哪些需要在线程中创建或者传递?你是用什么样的策略来实现的?
  2. Core Data:中多线程中处理大量数据同步时的操作。

在回答这两个问题之前,我们先看 Apple 是怎么告诉我们使用多线程的 Core Data 的,在最新的(2017-03-27) Core Data Programming Guide 中有一节 Concurrency with Core Data,它没有直接说如何使用多线程,只是说了 managed object context 在多线程中的两种使用模式:

In Core Data, the managed object context can be used with two concurrency patterns, defined by NSMainQueueConcurrencyType and NSPrivateQueueConcurrencyType.

NSMainQueueConcurrencyType is specifically for use with your application interface and can only be used on the main queue of an application.

The NSPrivateQueueConcurrencyType configuration creates its own queue upon initialization and can be used only on that queue. Because the queue is private and internal to the NSManagedObjectContext instance, it can only be accessed through the performBlock: and the performBlockAndWait: methods.

继续阅读